Ssuvin94
1 month ago

Need help in Laravel left join.

Posted 1 month ago by Ssuvin94

I shouldn't use raw query and it lead to this mess! Can you please help me to improve it.

@foreach($attributes as $attribute)
        {{ Form::bsText($attribute->name, $attribute->title, $customer->attributes()->find($attribute->id)->pivot->value ?? '') }}
@endforeach

Attribute Model:

public function customers()
{
    return $this->belongsToMany(Customer::class);
}

Customer Model:

public function attributes()
{
    return $this->belongsToMany(Attribute::class)->withPivot('value');
}

Controller:

public function show(Customer $customer)
{
    $attributes = Attribute::all();
    return view('admin.customers.show', compact('customer', 'attributes'));
}

Please sign in or create an account to participate in this conversation.