php - Laravel sounds like magic
684
I just recently started experimenting with laravel, lovely. One thing l dont understand though is how laravel knows my table l just added a model and the model isnt the exact table name, but just how does it manage to get my table,
Answer
Solution:
Reference to Eloquent Model Conventions:
Internally, Laravel does something like this.
So it will automatically try to look for the plural of your model name if no
$table
property is being set.Answer
Solution:
Laravel follows a Naming convention for Eloquent Classes and Tables.
From Laravel Website | Eloquent: Getting Started
Eg.
Class
User
will by default refers to Mysql Tableusers
(Camel Case to Snake Case and Plural).Class
NotificationsLog
will by default refers to Mysql Tablenotifications_logs
(Camel Case to Snake Case and Plural).But if you don't want to follow the convention then you can Mention the table name explicitly
Eg. If I want my Class
Plane
should refers toflights
table in Database then following code will workAnswer
Solution:
Actually the when you make a model it automatically make a table with it's plural you could change this by added the following code in the model
Please also check the name in the migration it should be same as the name you mentioned in the model class because it might show error while using eloquent class functions due to different table names