简体   繁体   中英

Passing several variables to blade view

I am using Laravel 5.5 and I am loading my data like the following:

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

However, in my view I get the following error:

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)"

The blade file has the following variable:

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

Any suggestions why I am getting the error? Do I load the variables wrongly into the view?

I appreciate your replies!

As get() gives you a collection of objects, not a single one, you should modify code:

use ->first() instead of ->get()

This will give you a single object with required properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM