drkwpgrdb092239314 2019-03-16 16:37
浏览 163
已采纳

错误 - 尝试获取非对象的属性 - Laravel

I am trying to show one single record based on id. And I am getting this error:

Trying to get property of non-object

Show function from ClientsController:

public function show( Clients $clients)
{
    $clients = Clients::find($clients->id);

    return view('clients.show', ['clients' => $clients]);

}

single show.blade view file

   @extends('layouts.app')

    @section('content')
     <h1>{{ $clients->name }}</h1>


    @endsection

Clients model:

class Clients extends Model
{
    //
    protected $fillable = [
        'name', 'type', 'user_id', 'sales_id', 'regions_id'
    ];
    public function user(){
        return $this->belongsTo('App\User');
    }
    public function region(){
        return $this->belongsTo('App\Regions');
    }
    public function sales(){
        return $this->belongsTo('App\Sales');
    }
}

Database: https://take.ms/7kuo4

  • 写回答

2条回答 默认 最新

  • doujuchuan9915 2019-03-16 17:28
    关注

    You should correct your model binding using the singular for the type hinted variable name, i.e.:

    public function show( Clients $client)
    {
        return view('clients.show', ['clients' => $client]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改