开发者问题收集

laravel 尝试获取空值上的非对象属性

2019-08-29
746

我试图从数据库获取数据,但当一列具有空值时,会出现此错误

Trying to get property of non-object

当assignTot为空时,问题会出现错误,如果有值,则一切正常。 我如何在我的控件中使用 return view('stations.show')->withStation('$station'); 这样的解决方案,返回多个

return view('admin.contracts.show', compact('contracts','asiTo','tranTo','replies'));

public function show($id){
    $contracts=Contracts::find($id)
        ->where('contracts.id',$id)
        ->join('destination','contracts.condesid','destination.id')
        ->join('department','contracts.condepid','department.id')
        ->join('users','contracts.conassigneto','users.id')
        ->select('contracts.*','destination.name as desName','department.name  as depName','users.name as assignTot')
        ->first();
    return view('admin.contracts.show', compact('contracts'));
}

Blade:

<h2><span class="profile-details-name-nn">{{ $contracts->conhotelname }}</span></h2>
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
    <div class="btn-group project-list-ad">
        <button class="btn btn-white btn-xs">{{ $contracts->constatus }}</button>
    </div>
    <div class="project-details-dt">
        @if($contracts->assignTot == null)
            <span>-</span>
        @else
            <span>{{ $contracts->assignTot }}</span>
        @endif
    </div>
</div> 
1个回答

您需要声明数组 $contracts 如下所示

public function show($id)
{
   $contracts = array();
   $contracts=Contracts::find($id)
    ->where('contracts.id',$id)
    ->join('destination','contracts.condesid','destination.id')
    ->join('department','contracts.condepid','department.id')
    ->leftjoin('users','contracts.conassigneto','users.id')
    ->select('contracts.*','destination.name as desName','department.name  as depName','users.name as assignTot')
    ->first();
     return view('admin.contracts.show', compact('contracts'));
  }
Leena Patel
2019-08-29