mardi 6 juin 2017

reuse mysql query code among few methods in same controller - Laravel 5.4

I have few queries which I would like to use it to few methods in the same controller.for example below code:

$lastlogin = User::select('lastlogin')->where('id',Auth::user()->id)->get()->pluck('lastlogin');
$bio = User::where('id',Auth::user()->id)->value('bio');
$photo = User::where('id',Auth::user()->id)->value('photo');
$notifications = Notification::where('created_at','>',$lastlogin)->get();
$status = User::where('id',Auth::user()->id)->value('search_status');

I need to call above query in 4 methods in UserController.

I thought of doing something like:

public function john_doe()
{
$lastlogin = User::select('lastlogin')->where('id',Auth::user()->id)->get()->pluck('lastlogin');
$bio = User::where('id',Auth::user()->id)->value('bio');
$photo = User::where('id',Auth::user()->id)->value('photo');
$notifications = Notification::where('created_at','>',$lastlogin)->get();
$status = User::where('id',Auth::user()->id)->value('search_status');
}

Then

UserController

public abc (){john_doe();}
public def (){john_doe();}
public ghi (){john_doe();}
public jkl (){john_doe();}

But I get an error. How do I do this so when I change the code in one place it reflects everywhere?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire