2017-02-16 149 views
0

我有一个“预订”具有与“市场”和“摊位”Laravel关系没有返回结果

我已在选择工作创建表单上,但该指数刀片显示不正确的关系。

应该采取“ID”,并在下面的

<td>{{$user->role ? $user->role->name : 'User has no role'}}</td> 

转换为“姓名”字段作为这与下面的型号代码正确显示: 公共职能作用(){

return $this->belongsTo('App\Role'); 
} 

,这是我的 “预订” 模式:

public function marketdate(){ 

    return $this->belongsTo('App\Markets'); 
} 

public function stallstype() { 

    return $this->belongsTo('App\Stalls'); 
} 

随着FO在index.blade llowing:

... 
<td>{{$booking->marketdate ? $booking->marketdate->date : '-' }}</td> 
<td>{{$booking->stallstype ? $booking->stallstype->name : '-'}}</td> 
... 

但是,这只是显示“ - ”,当它呈现在网页

标识的存储在数据库中是正确的,并做相关这是不正确显示。其他

一两件事,表架构如下:

摊位表:

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(); 
    }); 

的错误是:

at PhpEngine- >evaluatePath('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('Saturday 4th March', 'Saturday 5th April', 'Saturday 6th May', 'Saturday 7th June', 'Saturday 8th July', 'Saturday 8th July'), 'stalltype' => array('Preloved', 'Craft', 'Business'))) in compiled.php line 15361 
at 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('Saturday 4th March', 'Saturday 5th April', 'Saturday 6th May', 'Saturday 7th June', 'Saturday 8th July', 'Saturday 8th July'), 'stalltype' => array('Preloved', 'Craft', 'Business'))) in compiled.php line 15193 

这表明它正在返回阵列

+0

确实存在在那里的数据? – ggdx

+0

是的,我可以在数据库的“预订”表中看到ID,并且这些ID与相关表的数据库中的内容相对应,因为我在创建页面上的下拉列表使用相同的关系来填充 – Luke

+0

I' d说你没有得到任何预订数据,并且结果总是错误的。你如何获取你的$预订对象? – boroboris

回答

0

看来,Eloquent无法加载相关模型。当您使用列表()加载您的其他模型时,我假设您对业务模型也一样。

当您致电列表('name','id')时,您告诉Eloquent仅加载数据库中的这2个字段。因此,如果您也使用它来加载商业模式,那么您不会提取marketdate_id也不stallstype_id,这就是为什么Eloquent无法加载相关模型的原因。

确保外键的字段传递给列表()方法,如果你想加载相关机型:

Business::lists('name', 'id', 'stalltype_id', 'marketdate_id')->all();