Laravel Eloquent Nested Relationships. Storing rows in controller variable
I have some working queries that are not ideal and I’d like to try perform them the Eloquent way to tidy it up.
I think I’ve got the relationships defined correctly but I can’t figure out how to write the queries and store them in variables in the controller. I need to return these variables back to the view because I json encode them for use in JavaScript.
Here’s what I have in my controller:
public function show($idProject)
{
$project = ProjectsModel::with('user')->where('idProjects','=',$idProject)->first();
$objsettings = ObjSettingsModel::where('idObjSettings','=',$project['ObjSettingsID1'])->first();
$obj = ObjModel::where('idObjs','=',$objsettings['Objs_idObjs'])->first();
return view('project')->with('project',$project)->with('obj',$obj)->with('objsettings',$objsettings);
}
The naming conventions are a bit off so here’s what this does.
I pass the $idProject to the controller from a link on my index page where I’ve looped through and paginated all rows from the Projects table.
The first query finds the project row where it’s id (idProjects) matches the variable passed from the index page via the link (idProject). I’ve also successfully pulled the related user row from the user table using an eloquent relationship.
The next query pulls from an ObjSettings table which stores a number of settings values for an object which is shown on the page. I match the idObjSettings column of the ObjSettings table to the previously pulled $project[‘ObjSettingsID1’] which is essentially the foreign key in the projects table. There can be several ObjSettings for each Project.
The 3rd query pulls from the Obj table which stores details about an object. These are static details on objects such as name or size for example. I match the idObjs column to the previously pulled $objsettings[‘Objs_idObjs’] which is the foreign key in the ObjSettings table. One Obj can have many ObjSettings which are used in other Projects.
Here’s how I’m passing the php variables to JS:
<script>var obj = @json($obj);</script>
<script>var objsettings = @json($objsettings);</script>
Here are my relationships
ProjectsModel
public function user()
{
return $this->belongsTo('AppUser', 'Username', 'id');
}
public function objsettings()
{
return $this->hasMany('AppObjSettingsModel', 'idObjSettings', 'ObjSettingsID1' );
}
ObjSettingsModel
public function objs()
{
return $this->belongsTo('AppObjsModel', 'Objs_idObjs', 'idObjs');
}
public function projects()
{
return $this->belongsTo('AppProjectsModel', 'ObjSettingsID1', 'idObjSettings' );
}
ObjModel
public function objsettings()
{
return $this->hasMany('AppObjSettingsModel', 'idObjs', 'Objs_idObjs');
}
I’ve tried a whole range of queries such as:
$project = ProjectsModel::with('user')->with('objsettings.objs')->where('idProjects','=',$idProject)->first();
$objsettings = $project->objsettings;
$obj = $project->objsettings->objs;
but I keep running into issues such as “Property [objs] does not exist on this collection instance.” I suppose I’m returning multiple rows in this case? Any help would be appreciated.
from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-eloquent-nested-relationships-storing-rows-in-controller-variable/
via Lzo Media
No comments:
Post a Comment