dongrui6787 2017-02-16 13:53
浏览 12

Laravel关系没有结果

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.

It should take the "id" and convert it to the "name" field as in the below

 <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:

...
<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.

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-    >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\moowoosesources\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

Which shows that it is returning the array

  • 写回答

1条回答 默认 最新

  • duanli9591 2017-02-17 04:18
    关注

    It seems that Eloquent is not able to load related models. As you use lists() to load your other models I assume you do the same for Business model.

    When you call lists('name', 'id'), you're telling Eloquent to load only those 2 fields from the database. 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.

    Make sure that foreign key columns are passed to lists() method if you want to load related models:

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

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制