【发布时间】:2019-04-21 11:45:34
【问题描述】:
我不应该使用原始查询,这会导致混乱!你能帮我改进一下吗?
@foreach($attributes as $attribute)
{{ Form::bsText($attribute->name, $attribute->title, $customer->attributes()->find($attribute->id)->pivot->value ?? '') }}
@endforeach
属性模型:
public function customers()
{
return $this->belongsToMany(Customer::class);
}
客户模型:
public function attributes()
{
return $this->belongsToMany(Attribute::class)->withPivot('value');
}
控制器:
public function show(Customer $customer)
{
$attributes = Attribute::all();
return view('admin.customers.show', compact('customer', 'attributes'));
}
【问题讨论】:
标签: database laravel eloquent many-to-many