Laravel 5.4与资源的隐式路由模型绑定返回空对象 [英] Laravel 5.4 implicit route model binding with resource returns empty object

查看:218
本文介绍了Laravel 5.4与资源的隐式路由模型绑定返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel的路由模型绑定中遇到了一个非常奇怪的问题.

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

使用路线资源:

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

编辑条目的途径如下:

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

因此,根据我对隐式路由模型绑定的理解,vendor属性应允许直接访问该对象.

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

在我的Controller函数中,我可以毫无问题地获取实际的id.但是,当我尝试获取供应商对象时,结果为空.没有404,只是一个空结果,使模板失败并显示未定义变量:供应商".

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

推荐答案

更改此行:

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

对此:

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

这篇关于Laravel 5.4与资源的隐式路由模型绑定返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆