返回多个模型以查看

时间:2018-01-14 10:19:43

标签: php laravel laravel-5

我正在使用Laravel 5.5,我正在从函数中返回两个模型:

public function getDetails($id) {
...
$result = array($instruments->toJson(), $team->toJson());
}

现在我想在前端加载每个结果:

 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);
    }

我的控制台输出如下所示:

[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}]  

在我的index.blade.php文件中,我尝试按以下方式加载属性:

<img style="height: 32px; width: 32px;" src="{{ asset('images')}}/{{ $instrumentUnderEdit->image }}" /> {{ $instrumentUnderEdit->name }}

但是,我收到以下错误:

  

[2018-01-14 10:13:22] local.ERROR:试图获得财产   非对象{&#34;异常&#34;:&#34; [对象](ErrorException(代码:0):试图   在C:\ Users \ marcus \ Desktop \ Coding获取非对象的属性   项目\ demo_laravel_screener \应用\ HTTP \控制器\ InstrumentsController.php:203)

为什么我无法在$instrumentUnderEdit $instrumentUnderEdit->image [5.1, 3.5, 1.4, 0.2] Iris-Setosa [7.0, 3.2, 4.7, 1.4] Iris-versicolor 中查看Feature Result [[1.0 2.3 3.1],[2.4 6.3 9.6]] A [[1.5 3.3 5.1],[5.4 9.3 7.0]] B 变量的任何建议?

感谢您的回复!

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试将集合用作对象。

使用first()方法获取对象:

$teamUnderEdit->first()->firstname

如果要显示给定集合中的所有对象,请迭代它。

此外,而不是:

$res = $this->getDetails($id);
$instrument = $res['0']; //instrument
$team = $res['1']; //team

使用list()

list($instrument, $team) = $this->getDetails($id);