Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Another model than User

0

A lot of the basics are covered in Eloquent: Getting Started: Defining Models, as well as the other Eloquent ORM guides.

I don't know if there are many "complete" examples in the official docs, but models don't need much to be functional.

You are correct, setting $table is sufficient to connect the model to your database table. In fact, you don't even technically need to do that -- by default, Eloquent will just try to lowercase, underscore, and pluralize the class name and use that, which in this case would be correct. ('Post' => 'posts')

0

what u are sayin is described in this video: https://www.youtube.com/watch?v=U3PsPHqYRfQ

Laravel as u say, does bind the database table without the variable $table defined. I'm not sure how exactly, but until I encounter another error I'll leave it here.

thanks

0

muppbarn said:

Laravel as u say, does bind the database table without the variable $table defined. I'm not sure how exactly, but until I encounter another error I'll leave it here.

If you're curious about this functionality, you can see the code for it right here in Illuminate\Database\Eloquent\Model:

public function getTable()
{
    if (isset($this->table)) {
        return $this->table;
    }
    return str_replace('\\', '', Str::snake(Str::plural(class_basename($this))));
}

class_basename is a Laravel global helper, and plural and snake pluralize and snake_case strings, respectively.

This doesn't always work, but it's a pretty good guess for normal table name conventions.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

muppbarn muppbarn Joined 29 Mar 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.