Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model and table name conventions Laravel

Tags:

laravel

models

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.

like image 963
Harman Avatar asked Dec 22 '17 06:12

Harman


People also ask

What is model in laravel?

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.

How do I name a pivot table in laravel?

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 .

What is ORM in laravel?

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.

What is fillable in laravel?

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.


2 Answers

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.

like image 117
Alexey Mezenin Avatar answered Oct 21 '22 09:10

Alexey Mezenin


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

like image 27
Sapnesh Naik Avatar answered Oct 21 '22 09:10

Sapnesh Naik