【发布时间】:2014-01-08 19:08:10
【问题描述】:
我想用与CustomerEvent 相关的Customer 填充每个CustomerEvent。
当我循环遍历对象并调用 $customerevent->customer foreach 对象时,我可以获得与该事件相关的客户,但我可以填充主对象中的所有对象而不遍历每个事件吗?
我想做这样的事情:
$typeOne = CustomerEvent::typeOne()->get();
$customersWithTypeOne = $typeOne->Customers;
这是我的代码:
表 1:“事件”
id, customer_id
表 1“CustomerEvent”的模型:
<?php
class CustomerEvent extends Eloquent{
protected $guarded = ['id'];
public $table = "event";
protected $softDelete = true;
public function scopeTypeOne($query)
{
$followUps = $query->where('type_id', '=', '1');
}
public function Customers()
{
return $this->belongsTo('Customer', 'customer_id');
}
}
表 2:“客户”
id
表 2“客户”的模型:
<?php class Customer extends BaseModel{
protected $guarded = ['id'];
}
事件控制器:
public function index()
{
$typeOne = CustomerEvent::typeOne()->get();
$customersWithTypeOne = $typeOne->Customers;
dd($customersWithTypeOne);
}
【问题讨论】: