Anthony Rappa

Stuff I learned along the way

Laravel Eloquent naming convention guide

In order to use Eloquent the way it was intended, you need to follow the naming conventions that the core of Eloquent is expecting.

Well, you don’t have to, meaning you can override them in your models, but it’s a best practice and makes it easier and faster to develop if you know what the core of Laravel is expecting.

Here is how you should go about it, start with a model, this example is Post.

Mode: Post
Table: posts
Primary Key: id
Foreign Key on a Relationship: post_id (singular_model_id)

If you have a pivot table, it will be the singular model names snake cased, in alphabetical order:

post_user (Posts can have multiple users and users can have multiple posts)

Note: All columns should be snake_cased, all the relationship methods should be camelCased.

You can also override these conventions in the model:

Table: protected $table = 'my_posts';
Primary Key: protected primaryKey = 'custom_key';

Hope this helps!

«

© 2017 Anthony Rappa. Theme by Anders Norén.