我与存款模式有关系,但关系不起作用。
错误:尝试获取非对象的属性(查看:C:\ xamp \ htdocs \ assets \ core \ resources \ views \ user \ deposit-history.blade.php)
查看代码
<tbody>
@php $i = 0;@endphp
@foreach($deposit as $p)
@php $i++;@endphp
<tr>
<td>{{ $i }}</td>
<td width="10%">{{ date('d-F-Y h:s:i A',strtotime($p->created_at)) }}</td>
<td><span class="aaaa"><strong>{{ $p->plan->name }}</strong></span></td>
<td>#{{ $p->deposit_number }}</td>
<td>{{ $p->amount }} - {{ $basic->currency }}</td>
<td width="13%">{{ $p->percent }} %</td>
<td>{{ $p->time }} - times</td>
<td><span class="aaaa"><strong>{{ $p->compound->name }}</strong></span></td>
<td>
@if($p->status == 0)
<span class="label label-secondary"><i class="fa fa-spinner"></i> Running</span>
@else
<span class="label label-success"><i class="fa fa-check" aria-hidden="true"></i> Completed</span>
@endif
</td>
</tr>
@endforeach
</tbody>
型号代码
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Deposit extends Model
{
protected $table = 'deposits';
protected $fillable = ['deposit_number','user_id','plan_id','amount','status','percent','time','compound_id'];
public function plan()
{
return $this->belongsTo(Plan::class,'plan_id');
}
public function compound()
{
return $this->belongsTo(Compound::class,'compound_id');
}
public function user()
{
return $this->belongsTo(User::class,'user_id');
}
}
答案 0 :(得分:0)
在您看来,您有:
<td><span class="aaaa"><strong>{{ $p->plan->name }}</strong></span></td>
而且:
<td><span class="aaaa"><strong>{{ $p->compound->name }}</strong></span></td>
如果您始终拥有$p->plan
和$p->compound
,那么两行将有效。但是,如果你没有(例如null
)$p->plan
或$p->compound
,它将抛出异常(试图获取非对象的属性)。
那么,这些属性何时会null
?
首先,您需要确保plan_id
表中有compound_id
和deposits
。否则,关系属性将返回null
。
其次,如果null
或plan_id
为空/ null或compound_id
或{{1}中不存在的值,则可为plans
表。
解决方案是什么,即使它是compounds
?
如果您需要显示其他字段,即使您的null
或plan
为空,也可以将上述内容更改为以下行:
compound
和
<td><span class="aaaa"><strong>{{ isset($p->plan) ? $p->plan->name : '' }}</strong></span></td>