Hi, I have a route in my laravel app that uses route model binding to pass an instance of an eloquent model to a function. That function returns a view which receives the instance of the model as a parameter.
My model has a relationship which is called 'carer' and my blade file i need to access multiple times the relation so i use $mymodel->carer multiple times and i want to avoid each time i use $mymodel->carer makes a query to the database. I tried to load the relation before passing i to the view. Can someone help me? Thanks
#Accessing to a relation in a blade file
4 messages · Page 1 of 1 (latest)
well you can go into the controller that shows the blade file, get the data you need and then pass it in like:
$variable = \App\Models\carer::find (or whatever you do to fetch)
return view('home', compact('variable'));
this way you could go into the home blade file and could access the variable with $variable like <h1> {{ $variable }}</h1>
(if i understood your question correctly)
$myModel = MyModel::with('carer')->find(1);
return view('home', compact('myModel'));