0
votes

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
3
I believe it's because {vendor} is the ID of the vendor object. You'd have to use that ID to get the object from the DB. - Hanny
If you don't type hint it (i.e. function edit($vendor)), what do you get when you dd()? Assuming it's the ID, what do you get when you Vendor::find($vendor)? - Samsquanch
Are you correctly importing your App\Vendor model into your controller? - Simone Cabrino
App\Vendor is imported. For some reason it now does return an object as it should. But my template is still failing. - asto
Your template is likely failing because that's not how you use compact. Try compact('vendor'). - user1669496

3 Answers

1
votes

Change this line:

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

to this:

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

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);
0
votes

As an alternative to compacting an array, you could:

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