Laravel Method Injection


Implementing laravel's method injection magic on your projects.

Laravel Method Injection

Implementing laravel's method injection magic on your projects.


I just discovered Laravel's method injection and it dang rocks! To implement it on your projects, just simply call the methods using Laravel's Container.

To get the container instance, use laravel's app() helper. To support method injection, call your api using the container.

class DataTable {
    public function dataTable()
}

$object = new DataTable;
app()->call([$object, 'dataTable'])

If you need a custom parameter, just passed it as array on the second argument


class DataTable {
    public function dataTable($source)
}

$object = new DataTable;
$source = App\User::query();
app()->call([$object, 'dataTable'], compact('source'));

Reference:

https://mattstauffer.co/blog/laravel-5.0-method-injection

https://stackoverflow.com/questions/27930189/laravel-5-method-injection

 

Cheers!


  • Laravel
  • Tips And Tricks