【发布时间】:2019-07-12 19:26:34
【问题描述】:
所以我正在尝试使用 BelongsTo 来显示客户详细信息,这是我当前的代码:
我的控制器:
$assignees = assignee::latest()
->whereNull('timeout')
->paginate(10);
return view('assignees.index',compact('assignees'))
->with('i', (request()->input('page', 1) - 1) * 5);
分配表:
$table->string('original_filename')->nullable();
$table->string('custidno')->nullable();
$table->foreign('custidno')->references('custid')->on('customers');
$table->string('cardno')->nullable();
客户表:
Schema::create('customers', function (Blueprint $table) {
$table->increments('id');
$table->string('custid')->index()->unique();
分配模型:
public function cust()
{
return $this->belongsTo('App\Customer','custid');
}
在我看来:我有以下 for 循环,它显示“受让人表”我想用客户名称替换 custidno 字段,取自客户表。
index.blade.php:
<tr>
<td>{{ $assignee->id }}</td>
<td>{{ $assignee->datacenter }}</td>
<td>{{ $assignee->cust->name}}</td>
<td>{{ $assignee->refnumber }}</td>
我收到以下错误:
试图获取非对象的属性
【问题讨论】:
-
试试 {{ $assignee->cust()->name }}
-
我可以查看您的查找查询吗?
-
您必须在查询中指定“with”
-
可能你的
cust属性返回null...!你检查了吗? -
@KaushikMakwana,我已经从我的控制器中包含了一个 sn-p。