我有一個模型未返回相關模型,Call和CallType是我的模型,具有一對一關係。Laravel Eloquent不返回所有相關模型
呼叫模型
public function calltype(){
return $this->hasOne('App\CallType');
}
CALLTYPE模型
public function call(){
return $this->belongsTo('App\Call');
}
CallsController索引方法
$calls = Call::all();
$user_group = Group::find(Auth::user()->group_id);
return view('settings.calls.index', compact('calls', 'user_group'));
calls.blade.php
@foreach($calls as $call)
<tr>
<td>{{$call->call_code}}</td>
<td>{{$call->call_name}}</td>
<td>{{$call->calltype->type}}</td>
</tr>
@endforeach
個
電話遷移
Schema::create('calls', function (Blueprint $table) {
$table->increments('id');
$table->integer('call_type_id')->unsigned();
$table->string('call_name');
$table->string('call_code');
$table->date('start_date');
$table->date('end_date');
$table->string('target_audience')->nullable();
$table->text('description')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('call_type_id')->references('id')->on('call_types');
});
CallTypes遷移
Schema::create('call_types', function (Blueprint $table) {
//
$table->increments('id');
$table->string('type');
$table->timestamps();
});
我收到此錯誤:
Trying to get property of non-object
當我做dd($calls)
它沒有相關的模型。我究竟做錯了什麼?請幫忙。
更新我的帖子 – paranoid
但儘管如此,即使我這樣做,'calltype'爲空..當我做'$ call = Call :: all();'時,'calltype'應該被返回,但它不會被返回。 –