我有一个模型清单,该清单通过它的belongsTo('Model')关系继承,因此应固有地属于其相应模型所属的制造商。
这是来自我的清单模型:
public function model()
{
return $this->belongsTo('Model', 'model_id');
}
public function manufacturer()
{
return $this->belongsTo('Manufacturer', 'models.manufacturer_id');
/*
$manufacturer_id = $this->model->manufacturer_id;
return Manufacturer::find($manufacturer_id)->name;*/
}
和我的制造商型号:
public function listings()
{
return $this->hasManyThrough('Listing', 'Model', 'manufacturer_id', 'model_id');
}
public function models()
{
return $this->hasMany('Model', 'manufacturer_id');
}
我能够在视图中回显$ listing-> model-> name,但不能回显$ listing-> manufacturer-> name。这引发了错误。我尝试在Listing模型中注释掉两行只是为了获得效果,所以我可以回显$ listing-> manufacturer(),这行得通,但是不能正确建立它们之间的关系。我该怎么做呢?谢谢。
修改后的列表模型(感谢回答者):
public function model()
{
return $this->belongsTo('Model', 'model_id');
}
public function manufacturer()
{
return $this->belongsTo('Model', 'model_id')
->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id');
}
我找到了解决方案,但这不是很简单。我已经在下面发布了它,但是我首先发布了我认为是更好的解决方案。
您不应直接从清单中访问制造商,因为制造商仅适用于模型。尽管您可以从清单对象中快速加载制造商关系,但请参见下文。
class Listing extends Eloquent
{
public function model()
{
return $this->belongsTo('Model', 'model_id');
}
}
class Model extends Eloquent
{
public function manufacturer()
{
return $this->belongsTo('manufacturer');
}
}
class Manufacturer extends Eloquent
{
}
$listings = Listing::with('model.manufacturer')->all();
foreach($listings as $listing) {
echo $listing->model->name . ' by ' . $listing->model->manufacturer->name;
}
为了使您所需的解决方案起作用,需要花费一些时间。解决方案如下所示:
public function manufacturer()
{
$instance = new Manufacturer();
$instance->setTable('models');
$query = $instance->newQuery();
return (new BelongsTo($query, $this, 'model_id', $instance->getKeyName(), 'manufacturer'))
->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id')
->select(DB::raw('manufacturers.*'));
}
我首先使用查询并从中构建响应。我要创建的查询类似于以下内容:
SELECT * FROM manufacturers ma
JOIN models m on m.manufacturer_id = ma.id
WHERE m.id in (?)
通常通过执行以下操作创建的查询 return $this->belongsTo('Manufacturer');
select * from `manufacturers` where `manufacturers`.`id` in (?)
该?
会通过的值来代替manufacturer_id
从清单表列。该列不存在,因此将插入单个0,并且您永远不会返回制造商。
在我想重新创建的查询中,我受约束models.id
。通过定义外键,我可以轻松地在关系中访问该值。所以关系变成
return $this->belongsTo('Manufacturer', 'model_id');
这将产生与之前相同的查询,但是?
使用model_ids填充。因此,这将返回结果,但通常是不正确的结果。然后,我打算更改从中选择的基本表。该值是从模型派生的,因此我将传入的模型更改为Model
。
return $this->belongsTo('Model', 'model_id');
现在,我们已经模拟了模型之间的关系,这真是太好了,我还真没想到。但是至少现在,我可以加入到Manufacturers表中。所以我再次更新了关系:
return $this->belongsTo('Model', 'model_id')
->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id');
这使我们更近了一步,生成了以下查询:
select * from `models`
inner join `manufacturers` on `manufacturers`.`id` = `models`.`manufacturer_id`
where `models`.`id` in (?)
从这里开始,我想将查询的列限制为仅制造商列,为此,我添加了select规范。这使关系达到:
返回$ this-> belongsTo('Model','model_id')-> join('manufacturers','manufacturers.id','=','models.manufacturer_id')-> select(DB :: raw('manufacturers 。*'));
并查询到
select manufacturers.* from `models`
inner join `manufacturers` on `manufacturers`.`id` = `models`.`manufacturer_id`
where `models`.`id` in (?)
现在我们有一个100%有效的查询,但是从关系中返回的对象的类型Model
不是Manufacturer
。这就是最后一招的地方。我需要返回Manufacturer, but wanted it to constrain by the
模型模型table in the where clause. I created a new instance of Manufacturer and set the table to
并手动创建关系。
重要的是要注意,保存将不起作用。
$listing = Listing::find(1);
$listing->manufacturer()->associate(Manufacturer::create([]));
$listing->save();
这将创建一个新的制造商,然后更新listings.model_id
到新的制造商的ID。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句