Returning multiple models to view
I am using Laravel 5.5
and I am returning two models from a function:
public function getDetails($id) {
...
$result = array($instruments->toJson(), $team->toJson());
}
Now I would like to load each result in the frontend:
public function details($id)
{
$res = $this->getDetails($id);
$instrument = $res['0']; //instrument
$team = $res['1']; //team
Log::info("instrument");
Log::info($instrument);
Log::info("team");
Log::info($team);
Log::info($team->image);
return view('details')->with('instrumentUnderEdit', $instrument)->with('teamUnderEdit', $team);
}
My console output looks like the following:
[2018-01-14 10:13:22] local.INFO: instrument
[2018-01-14 10:13:22] local.INFO: {"id":1,"revisions_id":1,"name":"Bitcoin","symbol":"BTC","image":"bitcoin.png","sector":"sdfsf","country_of_origin":"Country of Origin","created_at":"2018-01-03 15:49:23","updated_at":"2018-01-03 15:49:23","revision_status":1}
[2018-01-14 10:13:22] local.INFO: team
[2018-01-14 10:13:22] local.INFO: [{"id":1400,"instruments_id":1,"revisions_id":1,"firstname":"adfasdf","lastname":"asdfasd","twitterAccount":"fasdfasdf","created_at":"2018-01-12 20:32:49","updated_at":"2018-01-12 20:32:49","name":"Bitcoin","symbol":"BTC","image":"bitcoin.png","sector":"sdfsf","country_of_origin":"Country of Origin","revision_status":1},{"id":1400,"instruments_id":1,"revisions_id":1,"firstname":"Sathosi","lastname":"Nakomoto","twitterAccount":"www.twitter.com\/Sathoshi","created_at":"2018-01-12 20:32:49","updated_at":"2018-01-12 20:32:49","name":"Bitcoin","symbol":"BTC","image":"bitcoin.png","sector":"sdfsf","country_of_origin":"Country of Origin","revision_status":1},{"id":1401,"instruments_id":1,"revisions_id":1,"firstname":"adfasdf","lastname":"asdfadsf","twitterAccount":"sadfadsf","created_at":"2018-01-12 20:57:05","updated_at":"2018-01-12 20:57:05","name":"Bitcoin","symbol":"BTC","image":"bitcoin.png","sector":"sdfsf","country_of_origin":"Country of Origin","revision_status":1},{"id":1401,"instruments_id":1,"revisions_id":1,"firstname":"Sathosi","lastname":"Nakomoto","twitterAccount":"www.twitter.com\/Sathoshi","created_at":"2018-01-12 20:57:05","updated_at":"2018-01-12 20:57:05","name":"Bitcoin","symbol":"BTC","image":"bitcoin.png","sector":"sdfsf","country_of_origin":"Country of Origin","revision_status":1},{"id":1401,"instruments_id":1,"revisions_id":1,"firstname":"Sathosi","lastname":"Nakomoto","twitterAccount":"www.twitter.com\/Sathoshi","created_at":"2018-01-12 20:57:05","updated_at":"2018-01-12 20:57:05","name":"Bitcoin","symbol":"BTC","image":"bitcoin.png","sector":"sdfsf","country_of_origin":"Country of Origin","revision_status":1}]
In my index.blade.php
file I am trying to load the property as the following:
<img style="height: 32px; width: 32px;" src="{{ asset('images')}}/{{ $instrumentUnderEdit->image }}" /> {{ $instrumentUnderEdit->name }}
However, I get the following error:
[2018-01-14 10:13:22] local.ERROR: Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at C:\Users\marcus\Desktop\Coding Projects\demo_laravel_screener\app\Http\Controllers\InstrumentsController.php:203)
Any suggestions why I cannot access in my view the $instrumentUnderEdit
variable with $instrumentUnderEdit->image
?
I appreciate your replies!
1 answer
-
answered 2018-01-14 10:22
Alexey Mezenin
Looks like you're trying to use a collection as an object.
Use the
first()
method to get an object:$teamUnderEdit->first()->firstname
If you want to display all objects in given collection, iterate over it.
Also, instead of this:
$res = $this->getDetails($id); $instrument = $res['0']; //instrument $team = $res['1']; //team
Use
list()
:list($instrument, $team) = $this->getDetails($id);