[英]Laravel Relationship returning no results
我有一本與“市場”和“故事”有關系的“書”
我在創建表單上使用了Selects,但是索引刀片未正確顯示。
它應該使用id
並將其轉換為name
字段,如下所示
<td>{{$user->role ? $user->role->name : 'User has no role'}}</td>
並使用以下模型代碼正確顯示:
public function role(){
return $this->belongsTo('App\Role');
}
這是我的“預訂”模型 :
public function marketdate(){
return $this->belongsTo('App\Markets');
}
public function stallstype() {
return $this->belongsTo('App\Stalls');
}
在index.blade上具有以下內容 :
...
<td>{{$booking->marketdate ? $booking->marketdate->date : '-' }}</td>
<td>{{$booking->stallstype ? $booking->stallstype->name : '-'}}</td>
...
但這在呈現頁面時僅顯示“-”
存儲在數據庫中的ID正確且確實相關,只是顯示不正確。
另一件事,表架構如下:
檔位表 :
Schema::create('stalls', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('cost');
$table->timestamps();
});
市場表 :
Schema::create('markets', function (Blueprint $table) {
$table->increments('id');
$table->string('date');
$table->integer('is_active');
$table->timestamps();
});
錯誤是:
在PhpEngine-
validatePath('C:\\ xampp \\ htdocs \\ moowoos \\ storage \\ framework \\ views / 3bcb71b43fffee7f9 a9edf18bfb397ab94380507.php',array('__ env'=> object(Factory),'app'=> object(Application),'errors'= > object(ViewErrorBag),'bookings'=> object(Collection),'markets'=> array('3月4日星期六','4月5日星期六','5月6日星期六','6月7日星期六','8月星期六July','Saturday 8th July'),'stalltype'=> array('Preloved','Craft','Business')))))在CompilerEngine-> get('C:\\ xampp \\ htdocs \\ moowoos \\ resources \\ views / admin / bookings / index.blade.php',array('__ env'=> object(Factory),'app'=> object(Application),'errors'=> object(ViewErrorBag), 'bookings'=> object(Collection),'markets'=> array('3月4日星期六','4月5日星期六','5月6日星期六','6月7日星期六','7月8日星期六','6月8日星期六July'),'stalltype'=> array('Preloved','Craft','Business')))))在configure.php第15193行
這表明它正在返回array
似乎Eloquent無法加載相關模型。 當您使用lists()加載其他模型時,我假設您對業務模型也是如此。
當您調用list('name','id')時 ,您是在告訴Eloquent僅從數據庫中加載這兩個字段。 因此,如果您也使用它來加載業務模型,則不會獲取marketdate_id或stallstype_id ,這就是Eloquent無法加載相關模型的原因。
如果要加載相關模型,請確保將外鍵列傳遞給list()方法:
Business::lists('name', 'id', 'stalltype_id', 'marketdate_id')->all();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.