I want to know model and table conventions in Laravel.
I have table users and user_address now I do not want to use
protected $table = "user_address"
property. Please suggest me the right convention in model or table.
Laravel Create Model is an MVC based PHP system. In the MVC architecture, 'M' stands for 'Model'. A model is used as a way for questioning data to and from the table within the database. Laravel gives a basic way to do that using Eloquent ORM where each table incorporates a Model to interact with it.
Laravel's naming convention for pivot tables is snake_cased model names in alphabetical order separated by an underscore. So, if one model is Feature , and the other model is Product , the pivot table will be feature_product .
Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.
In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.
If the name of the model is UserAddress
, the name of the table should be plural. So it will be user_addresses
.
Read more about Laravel naming conventions here.
If your model name is User.php then Laravel expects class 'User' to be inside that file. It also expects users table for User model (plural form).
Similiarly, If you have a user_address
model your table should be user_addresses
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With