Laravel 5.4 implicit route model binding with resource returns empty object

岁酱吖の 提交于 2019-12-25 04:29:16

问题


I'm stuck on a very odd issue with laravel's route model binding.

Using a route resource:

Route::resource('vendors', 'VendorController');

The route for editing an entry is the following:

GET|HEAD | admin/vendors/{vendor}/edit | vendors.edit | App\Http\Controllers\VendorController@edit | web,auth.admin

So from my understanding of implicit route model binding, the vendor attribute should allow accessing the object directly.

In my Controller function I can get the actual id with no problem. But when I try to get the vendor object, the result is empty. No 404, but just an empty result, making the template fail with "Undefined variable: vendor".

public function edit(Vendor $vendor)
{
    dd($vendor);
    return view('admin.vendor.edit', compact($vendor));
}

Can anybody point me in the right direction?

Update: For some reason the route model binding is now working. However the template still throws an error, saying vendor is undefined.

@extends('admin.layout')

@section('content')

{!! Form::model($vendor, ['method' => 'PATCH', 'url' => 'admin/vendors'.$vendor->id]) !!}
    @include ('admin.vendor._form', ["submitButtonText" => "Änderungen speichern"])
{!! Form::close() !!}

@include ('_errors')

@endsection

回答1:


Change this line:

return view('admin.vendor.edit', compact($vendor));

to this:

return view('admin.vendor.edit', compact('vendor'));



回答2:


I believe it's because {vendor} is the ID of the vendor object.

You'd have to get that object using that ID I believe.

Something like this:

Vendor::find($vendor);



回答3:


As an alternative to compacting an array, you could:

public function edit(Vendor $vendor)
{
   return view('admin.vendor.edit')->withVendor($vendor);
}


来源:https://stackoverflow.com/questions/42633286/laravel-5-4-implicit-route-model-binding-with-resource-returns-empty-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!