No repeating Eloquent query statements in the mathods of a controller
I have an Eloquent model query with quite a few statements which is repeated the same in almost all the methods of a controller. Is there a way to save it somewhere and call it with a name into the methods of the controller with a single line?
Thank you for your suggestion!
The solution 1 seems to be more close to what I have in my mind but for the sake of good and clean modern programming I try not to use it.
In solution 2 we will try to create functions into the model which will be available to be called in whatever controller. ButI have not used scopes in models. Besides reading the documentation I have no real experience in working with scopes. Would you mind giving me any good working example?
class PagamController extends Controller
{
public function index(Request $request)
{
$currentefimeriosid = auth()->user()->relates_to_efimerios_id;
$efimeriosdata = Efimer::findOrFail($currentefimeriosid);
$currentenoriaid = $efimeriosdata->unit->id;
$pagams = Pagam::with('unit')
->where('unit_id',$currentenoriaid)
->orderBy('arprot')
->get();
$minyear = Pagam::whereNotNull('etoscreated')->min("etoscreated");
$maxyear = Pagam::whereNotNull('etoscreated')->max("etoscreated");
return view('pagams.index', compact('pagams','minyear','maxyear'));
}```
The statements which are repeated into somae methods of the Controller are
``` $pagams = Pagam::with('unit')
->where('unit_id',$currentenoriaid)
->orderBy('arprot')
->get();
$minyear = Pagam::whereNotNull('etoscreated')->min("etoscreated");
$maxyear = Pagam::whereNotNull('etoscreated')->max("etoscreated");```
The Pagam is the model
Thanks for answering. If I am not mistaken, View Composers are a very good solution for sharing the same data in multiple blade views. I have a different problem: I want to be able to use the same Eloquent query in whatever controller's methods.
Since the code you have shown only passes those to the view, then it makes sense to use a view composer. If you just to use them for some logic in other methods in the controller, then I would set them in a constructor method.
class PagamController extends Controller
{
protected int $minYear;
protected int $maxYear;
public function __construct()
{
$this->minYear = Pagam::whereNotNull('etoscreated')->min("etoscreated");
$this->maxYear = Pagam::whereNotNull('etoscreated')->max("etoscreated");
}
The statements which are repeated into somae methods of the Controller are…
@adamnet Then you clearly have some sort of partial in your layout template if you need to copy-and-paste this query in multiple controllers. And you’ve already been told that the solution to that problem is view composers. So why do you keep creating posts with the same question when you’ve already been given the answer?
So, create a view composer with the query, and bind it to the relevant view.
Service class (with static functions) might also be right for your use case.
Repeating your code is always bad practice, try to resolve duplicate code by deciding what 'domain' it belongs to and write a appropriate Class object/function name.
If this code needs to appear in multiple methods of the same controller, then it sounds like your controller is doing too much, and maybe you have a lot of methods that go beyond the recommended resource methods.
If this concerns data than needs to be in every view then it sounds like it is part of the layout and not related to the operations of the controller. In which case I would probably use a class based blade component, and put your queries in the class.
If the data is more for the layout of the site, then a view composer is the way.
When I have several times the same code repeated in several method in the same (or not) controller, I sometime use a service class and I inject this class into the controller constructor.