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'
)
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
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community