【问题标题】:Passing several variables to blade view将多个变量传递给刀片视图
【发布时间】:2018-01-14 03:41:34
【问题描述】:

我正在使用 Laravel 5.5,我正在加载我的数据,如下所示:

public function details($id)
{

    $instrument = Instruments::join('revisions', 'revisions.id', '=', 'instruments.revisions_id')
        ->where([
            ['revisions.revision_status', '=', '1'],
            ['instruments.id', '=', $id],
        ])
        ->orderBy('instruments.name')
        ->get();

    $team = Team::join('instruments', 'teams.instruments_id', '=', 'instruments.id')
        ->join('revisions', 'revisions.id', '=', 'teams.revisions_id')
        ->where([
            ['revisions.revision_status', '=', '1'],
            ['instruments.id', '=', $id],
        ])
        ->orderBy('instruments.name')
        ->get();


    return view('details')->with('instrumentUnderEdit', $instrument)->with('teamUnderEdit', $team);
}

但是,在我看来,我收到以下错误:

ErrorException thrown with message "Property [image] does not exist on this collection instance. (View: C:\Users\admin\Desktop\Projects\demo_laravel_screener\resources\views\details.blade.php)"

刀片文件有以下变量:

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

我收到错误的任何建议?我是否将变量错误地加载到视图中?

感谢您的回复!

【问题讨论】:

  • 所以get() 给你一个集合,而不是单个对象。
  • 你想要所有行还是单行?
  • @user2486 我想将所有行传递给前端。有什么建议吗?
  • 你需要循环 woth foreach

标签: php laravel-5 blade


【解决方案1】:

由于get() 为您提供collection 对象,而不是单个对象,您应该修改代码:

使用-&gt;first() 而不是-&gt;get()

这将为您提供具有所需属性的单个对象。

【讨论】:

  • 感谢您的回答!如何实际将对象集合传递给前端?这是一样的吗?
  • 是的,相同,并且在模板中迭代此集合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2018-09-16
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多