如何模仿与逗号分隔的ID的雄辩关系 [英] How to mimic Eloquent relationship with comma separated ids
问题描述
我有两个表,顺序和图层。在我的订单表我保存一个数组在layer_id,我已将其设置为varchar。我先爆炸它,我显示的记录。我想做的是显示来自layers表的记录,例如来自layer_name列的名称。我也设置了他们的关系。非常感谢任何建议。
I have two tables, orders and layers. In my orders table i save an array in layer_id which i have set it to varchar. I explode it first and i display the records. What i wanna do is to display the records from layers table , for example names from layer_name columns . I have also set their relationships . I would appreciate any suggestions.
我的控制器:
public function getCheckout(){
$orders = Order::get();
return View::make('checkout')->with('orders', $orders);
}
我的观点:
@forelse($orders as $order)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $order->style_id }}</h3>
</div>
<div class="panel-body">
<p>Layers you chose</p>
<table class="table">
<tr>
<td>ID</td>
@foreach(explode(',', $order->layer_id) as $layer)
<td>{{ $layer }}</td>
@endforeach
</tr>
</table>
</div>
<div class="panel-footer"><button class="btn btn-primary">Confirm</button></div>
</div>
@empty
<p>Nothing to display</p>
@endforelse
我的模型
Class Order extends Eloquent {
public function layer() {
return $this->belongsTo('Layer', 'layer_id');
}
}
推荐答案
第一个建议是标准化您的数据库。在Eloquent ORM的上下文中,你不能为此使用内置关系。
My first suggestion would be normalize your db. In the context of Eloquent ORM you cannot use built-in relationships for this.
但是为了好奇,这里是你应该做的:
However for the sake of curiosity, here's what you should do:
// 1. Use layers like eloquent dynamic property
$order->layers; // collection of Layer models
// 2. Call the query to fetch layers only once
$order->layers; // query and set 'relation'
// ... more code
$order->layers; // no query, accessing relation
// 3. Further query layers like you would with relationship as method:
$order->layers()->where(..)->orderBy(..)->...->get();
// 4. Associate layers manually providing either array or string:
$order->layer_ids = '1,5,15';
$order->layer_ids = [1,5,15];
但您不能:
But you can't:
// 1. Eager/Lazy load the layers for multiple orders
$orders = Order::with('layers')->get(); // WON'T WORK
// 2. Use any of the relationship methods for associating/saving/attaching etc.
$order->layers()->associate(..); // WON'T WORK
但这里是你可以做的(我建议重命名 layer_id
到 layer_ids
,所以这是不言自明的,我的例子涵盖了变化):
But here's what you can do (I suggest renaming layer_id
to layer_ids
so it is self-explanatory, and my example covers that change):
/**
* Accessor that mimics Eloquent dynamic property.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getLayersAttribute()
{
if (!$this->relationLoaded('layers')) {
$layers = Layer::whereIn('id', $this->layer_ids)->get();
$this->setRelation('layers', $layers);
}
return $this->getRelation('layers');
}
/**
* Access layers relation query.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function layers()
{
return Layer::whereIn('id', $this->layer_ids);
}
/**
* Accessor for layer_ids property.
*
* @return array
*/
public function getLayerIdsAttribute($commaSeparatedIds)
{
return explode(',', $commaSeparatedIds);
}
/**
* Mutator for layer_ids property.
*
* @param array|string $ids
* @return void
*/
public function setLayersIdsAttribute($ids)
{
$this->attributes['layers_ids'] = is_string($ids) ? $ids : implode(',', $ids);
}
可以做到这一点在你看来。它遵循你当前的代码,但显然是远远不是我的建议;)
edit: Of course you could do simply this in your view. It adheres to your current code, but obviously is far from what I suggest ;)
@foreach (Layer::whereIn('id', explode(',', $order->layer_id))->get() as $layer)
{{ $layer->whatever }}
这篇关于如何模仿与逗号分隔的ID的雄辩关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!