ErrorException(E_ERROR)尝试获取非对象的属性“id”(查看: - laravel 5.5

时间:2018-06-12 09:22:05

标签: php mysql laravel

我试图从显示的数据列表中打开我的编辑表单但是我收到了上述错误。这是我的控制器显示页面:

public function edit($id)
{
    $assets = Asset::all();
    $tenants = Tenant::all();
    $tenantProperty = TenantProperty::find($id);
    return view('agent/edit_tenantProperty', compact('tenantProperty', 'assets', 'tenants'));
}

我的观点

<form method="post" action="{{ url('agent/edit_tenantProperty/'. $tenantProperty->id) }}" data-toggle="validator">
                {{ csrf_field() }}
                {{ method_field('PATCH') }}

虽然路线是这个

Route::get('edit_tenantProperty/{id}', [ 'as' => 'agent/edit_tenantProperty', 'uses' => 'TenantPropertyController@edit']);

请帮助我,我该如何解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:1)

您必须检查控制器中是否存在$tenantProperty,如果不存在则发送未找到的响应。

public function edit($id)
{
    $assets = Asset::all();
    $tenants = Tenant::all();
    $tenantProperty = TenantProperty::find($id);

    if (empty($tenantProperty)) { 
        // returny tenantProperty not fount response
    }

    return view('agent/edit_tenantProperty', compact('tenantProperty', 'assets', 'tenants'));
}

或检查$tenantProperty是否存在于视图

@if ($tenantProperty)
   <form method="post" action="{{ url('agent/edit_tenantProperty/'. $tenantProperty->id) }}" data-toggle="validator">
      {{ csrf_field() }}
      {{ method_field('PATCH') }}
@endif