如何设置雄辩的关系属于Laravel的另一个模型? [英] How to set Eloquent relationship belongsTo THROUGH another model in Laravel?
问题描述
这是来自我的列表模型:
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制造商:: 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-> manufacturer(),这样会有效,但是这并不能正确建立他们的关系。我该如何做?谢谢。
修改列表模式(感谢回答者):
public function model()
{
return $ this-> belongsTo('Model','model_id');
}
public function manufacturer()
{
return $ this-> belongsTo('Model','model_id')
- > join('manufacturer','manufacturer.id','=','models.manufacturer_id');
}
这不是很直截了当。我已经发布在下面,但我发布了我认为是更好的解决方案。
您不应该能够直接从列表中访问制造商,因为制造商仅适用于型号。虽然您可以从列表对象中加载制造商关系,如下所示。
类列表扩展Eloquent
{
public function model()
{
return $ this-> belongsTo('Model','model_id');
}
}
class模型扩展Eloquent
{
public function manufacturer()
{
return $ this-> ;属于关联( '制造商');
}
}
class制造商扩展Eloquent
{
}
$ listing = Listing :: with .manufacturer') - >所有();
foreach($ listing as $ listing){
echo $ listing-> model-> name。 'by'。 $ listing->&模型 - GT;与制造商>名称;
}
花了一点点时间,让您的请求解决方案工作。解决方案如下所示:
public function manufacturer()
{
$ instance = new制造商);
$ instance-> setTable('models');
$ query = $ instance-> newQuery();
return(new BelongsTo($ query,$ this,'model_id',$ instance-> getKeyName(),'manufacturer'))
- > join(' 'manufacturer.id','=','models.manufacturer_id')
- > select(DB :: raw('manufacturer。*'));
}
我从开始处理查询并构建响应。我想要创建的查询是:
SELECT * FROM manufacturer ma
JOIN models m在m.manufacturer_id = ma.id
WHERE m.id in(?)
通常通过 return $ this-> belongsTo('Manufacturer')创建的查询;
<$ p $ (?)
?
将由列表表中的 manufacturer_id
列替换。此列不存在,因此将插入一个0,您将永远不会返回制造商。
在我想要重新创建的查询中,我被 models.id
限制。我可以通过定义外键轻松访问我的关系中的值。所以关系成为
return $ this-> belongsTo('Manufacturer','model_id');
这样产生与之前相同的查询,但填充?
与model_ids。所以这样会返回结果,但通常结果不正确。然后我的目的是改变我正在选择的基表。这个值是从模型中导出的,所以我将模型中传递的模型更改为模型
。
code> return $ this-> belongsTo('Model','model_id');
我们现在模仿了模型关系,所以我没有真正的任何地方。但至少现在,我可以加入制造商表。所以再次更新了关系:
return $ this-> belongsTo('Model','model_id')
- > join('manufacturer','manufacturer.id','=','models.manufacturer_id');
这让我们一步一步,生成以下查询:
select * from`models`
内部连接`manufacturer`在`manufacturer`.`id` =`models`.`manufacturer_id`
(?)
从这里,我想限制列我正在询问制造商列,为此我添加了选择规范。这带来了关系:
return $ this-> belongsTo('Model','model_id')
- > join('manufacturer','manufacturer 。$$$$$$$$$$并得到查询
选择制造商*从`models`
内部连接` .`id` =`models`.`manufacturer_id`
其中`models`.`id` in(?)
现在我们有一个100%有效的查询,但是从关系返回的对象的类型是模型
不是制造商
。这就是最后一点诡计的地方,我需要返回一个制造商,但是希望它通过where子句中的
模型表约束。我创建了一个新的制造商实例,并将表设置为
models`并手动创建关系。
重要的是要注意, strong> save将无法正常工作。
$ listing = Listing :: find(1);
$ listing-> manufacturer() - > associate(Manufacturer :: create([]));
$ listing-> save();
这将创建一个新的制造商,然后更新 listings.model_id
到新的制造商的id。
I have a model Listing that inherits through its belongsTo('Model') relationship should inherently belong to the Manufacturer that its corresponding Model belongs to.
Here's from my Listing 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;*/
}
and my Manufacturer model:
public function listings()
{
return $this->hasManyThrough('Listing', 'Model', 'manufacturer_id', 'model_id');
}
public function models()
{
return $this->hasMany('Model', 'manufacturer_id');
}
I am able to echo $listing->model->name in a view, but not $listing->manufacturer->name. That throws an error. I tried the commented out 2 lines in the Listing model just to get the effect so then I could echo $listing->manufacturer() and that would work, but that doesn't properly establish their relationship. How do I do this? Thanks.
Revised Listing model (thanks to answerer):
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');
}
I found a solution, but it's not extremely straight forward. I've posted it below, but I posted what I think is the better solution first.
You shouldn't be able to access manufacturer directly from the listing, since manufacturer applies to the Model only. Though you can eager-load the manufacturer relationships from the listing object, see below.
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;
}
It took a bit of finagling, to get your requested solution working. The solution looks like this:
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.*'));
}
I started off by working with the query and building the response from that. The query I was looking to create was something along the lines of:
SELECT * FROM manufacturers ma
JOIN models m on m.manufacturer_id = ma.id
WHERE m.id in (?)
The query that would be normally created by doing return $this->belongsTo('Manufacturer');
select * from `manufacturers` where `manufacturers`.`id` in (?)
The ?
would be replaced by the value of manufacturer_id
columns from the listings table. This column doesn't exist, so a single 0 would be inserted and you'd never return a manufacturer.
In the query I wanted to recreate I was constraining by models.id
. I could easily access that value in my relationship by defining the foreign key. So the relationship became
return $this->belongsTo('Manufacturer', 'model_id');
This produces the same query as it did before, but populates the ?
with the model_ids. So this returns results, but generally incorrect results. Then I aimed to change the base table that I was selecting from. This value is derived from the model, so I changed the passed in model to Model
.
return $this->belongsTo('Model', 'model_id');
We've now mimic the model relationship, so that's great I hadn't really got anywhere. But at least now, I could make the join to the manufacturers table. So again I updated the relationship:
return $this->belongsTo('Model', 'model_id')
->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id');
This got us one step closer, generating the following query:
select * from `models`
inner join `manufacturers` on `manufacturers`.`id` = `models`.`manufacturer_id`
where `models`.`id` in (?)
From here, I wanted to limit the columns I was querying for to just the manufacturer columns, to do this I added the select specification. This brought the relationship to:
return $this->belongsTo('Model', 'model_id') ->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id') ->select(DB::raw('manufacturers.*'));
And got the query to
select manufacturers.* from `models`
inner join `manufacturers` on `manufacturers`.`id` = `models`.`manufacturer_id`
where `models`.`id` in (?)
Now we have a 100% valid query, but the objects being returned from the relationship are of type Model
not Manufacturer
. And that's where the last bit of trickery came in. I needed to return a Manufacturer, but wanted it to constrain by the
modelstable in the where clause. I created a new instance of Manufacturer and set the table to
models` and manually create the relationship.
It is important to note, that saving will not work.
$listing = Listing::find(1);
$listing->manufacturer()->associate(Manufacturer::create([]));
$listing->save();
This will create a new Manufacturer and then update listings.model_id
to the new manufacturer's id.
这篇关于如何设置雄辩的关系属于Laravel的另一个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!