A Guide To Not Repeat Common Laravel Relationships In Model Using Traits

Solaiman Khan
3 min readAug 21, 2021

Defining relationships in Laravel is something that every Laravel developer probably has done more than once. But if you define same relationships in almost every model, that would be repetitation of work, what if we have a common file and it use by every model whether it has some common properties to use or on demand use to be precise.

#Example:

Most of the cases your tables might need a ‘Created By’ column to identify who created it and when subsquently like ‘Created At’ for that you need to add this two column to your migration and to access the full eloquent feature you often need to define relation in every model for that and that a whole repeated work to do. What if we have a common file with the desire relationship defined and use in every model to access the relation on to go. We just have to write once and that it. Use and enjoy in every model.

Lets dive into it

#Step 1:

  1. Create a directory on your app directory name ‘Traits’ here you will put all your traits files.
    note: Its a personal preference, if you wish to organize then do it, otherwise the app directory is fine.
create a dir to app directory.

#Step 2:

Create a php file name ‘YourTraitFileName.php’ in my case its CommonTrait.php

  1. Add a namespace (automated in few IDE).
  2. Decleare a trait name ‘YourTraitName’ in my case its ‘CommonTrait’.

#Step 3:

  1. Define a public function ‘relation name’, in my case ‘created_user’.
  2. Now declear a hasOne relationship, in this case we have a User model which is ships with laravel by defaults.
    note: As Eloquent determines the foreign key of the relationship based on the parent model name, so we assume our ParentModel*has a column name user_id.*HereParentModelis the model where we will use this trait. Also make sure the Models are relationships are imported at the top.

3. But in my case i have a column name created_by in my ParentModel,to use that you have to say it explicitly. Like below image.

As laravel define

return $this->hasOne(Model, 'foreign_key', 'local_key');

#Step 4:

  1. Now use in your model. In my case its Invoice Model.
    note: ParentModel which is Invoice in my case.

#Step 5:

  1. Done!
  2. Now use like as usual you use it. Wherever you need that use relationship just use the trait in your model and bamm.

#Additional:

  1. You can also use this trait in many common issues of your model like, to get your table name you can use like this. This getTableName function will give you the table name.

2. And many more common properties, i think you get the idea.

I hope that this article gave you a short yet comfortable solution to get rid of code repeating in different models and save a little bit of time. Feel free to leave a comment if you have any feedback, questions and thanks for reading.

Jazāk Allāhu Khayran!

--

--

Solaiman Khan

Slave to Allah, a son, husband, brother, friend, and human being with humanity