This is my first post (hope not the last) ๐Ÿ”ฎ and I want to talk about a feature in PHP that helped me to improve and change the routine when I was developing Laravel projects ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

The basic story

A long time ago I was developing a dashboard. It had many sections, such as:

  • users
  • products
  • providers
  • categories

and so on...

All this sections have in common the: CRUD (Create, Read, Update & Delete), and itโ€™s ok, Laravel makes it very easy to handle these actions out of the box.

That's why developers love it, don't you? ๐Ÿงก


Just reviewing

  • For storing all the data from the form, we can use save()
  • For deleting, we use delete() or destroy() ... depends on what you need to do.
  • For updating we can also use save() (but my personal experience was that I had to specify which fields I wanted to update and it became a bit tedious. ๐Ÿ˜’)

And I'm not saying that working like this is bad ๐Ÿ™…๐Ÿปโ€โ™€๏ธ but I wondered, is there an option to make my updates generic, no matter the class? ๐Ÿคท๐Ÿปโ€โ™€

โ€ฆand thatโ€™s when I started using traits. โœจ ๐Ÿ™Œ๐Ÿป โœจ

Traits allow us to group functionalities, use them in different classes, keeping code maintainable and following the DRY principle.

In Laravel we can make them as a simple class, using the word trait.


The example

In this case Iโ€™ll make a trait to make a generic method to update my forms, no matter the class.

So, first I create a new folder inside /app with the name /Traits and my class UpdateGenericClass.php

/app/Traits/UpdateGenericClass.php

๐Ÿง™๐Ÿปโ€โ™€๏ธ Don't forget to modify the namespace including the reference of the Traits folder.

In that class, I make my first method: updateData()

  • First we code a static function that receives a parameter. In this case itโ€™s $id (you can receive other params too)
  • Then in my return with self:: I make the query based on the parameter $id, so I can update all my requests, except the token field.
<?php  

namespace App\Traits;

use Exception;
use Illuminate\Http\Request;

trait UpdateGenericClass {
  
  public static function updateData($id)
  {
    return self::where('id', $id)->update(request()->except('_token'));
  }
  
}

?>

To use my trait ๐Ÿ’๐Ÿปโ€โ™€๏ธ

  • In the model to use, we include and call the trait
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\\Traits\UpdateGenericClass;

class Page extends Model
{
    use UpdateGenericClass;
    protected $table = "pages";

}

Then in controller ๐Ÿ™‹๐Ÿปโ€โ™€๏ธ

  • I reference the traitโ€™s method; passing it the $id that I want to update.
  • My function will know which class is "using it" (in this case: the Page class).
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Page;

class PageController extends Controller
{
  
    public function update(Request $request, $id)
    {
        Page::updateData($id);
        return redirect()->route('pages.index');
    }
  
}

As the method of the trait is called in the context of the controller, we only make use of request() (refer to UpdateGenericClass) which contains the information to update.

And that's it, you can save the data of your forms using traits.

Well, this was a very short post. And itโ€™s one of many ways of doing things with Laravel, so recipes are always infinite. โ˜•๏ธ ๐Ÿ˜‰