简体   繁体   English

Laravel关系没有结果

[英]Laravel Relationship returning no results

I have a "booking" that has relationships with the "markets" and "stalls" 我有一本与“市场”和“故事”有关系的“书”

I have the Selects working on the create form but the index blade is not displaying correctly. 我在创建表单上使用了Selects,但是索引刀片未正确显示。

It should take the id and convert it to the name field as in the below 它应该使用id并将其转换为name字段,如下所示

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

And this displays correctly with the following Model code: 并使用以下模型代码正确显示:

public function role(){

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

and this is my "Bookings" Model : 这是我的“预订”模型

public function marketdate(){

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

public function stallstype() {

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

With the following on the index.blade : index.blade上具有以下内容

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

But this just shows the "-" when it renders the page 但这在呈现页面时仅显示“-”

the ID's that are stored in the database are correct and do correlate it is just not displaying correctly. 存储在数据库中的ID正确且确实相关,只是显示不正确。

One other thing, the table schemas are as follows: 另一件事,表架构如下:

Stalls table : 档位表

Schema::create('stalls', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('cost');
        $table->timestamps();
    });

Markets table : 市场表

  Schema::create('markets', function (Blueprint $table) {
        $table->increments('id');
        $table->string('date');
        $table->integer('is_active');
        $table->timestamps();
    });

The error is: 错误是:

at PhpEngine- 在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 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行

Which shows that it is returning the array 这表明它正在返回array

It seems that Eloquent is not able to load related models. 似乎Eloquent无法加载相关模型。 As you use lists() to load your other models I assume you do the same for Business model. 当您使用lists()加载其他模型时,我假设您对业务模型也是如此。

When you call lists('name', 'id') , you're telling Eloquent to load only those 2 fields from the database. 当您调用list('name','id')时 ,您是在告诉Eloquent仅从数据库中加载这两个字段。 So, if you use that to load Business models as well, you're not fetching marketdate_id nor stallstype_id , that's why Eloquent is not able to load the related models. 因此,如果您也使用它来加载业务模型,则不会获取marketdate_idstallstype_id ,这就是Eloquent无法加载相关模型的原因。

Make sure that foreign key columns are passed to lists() method if you want to load related models: 如果要加载相关模型,请确保将外键列传递给list()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM