Laravel - Many to Many - Model binding

confm

I need some help.

I have these tables: users, buys and codecs. I have a many-to-many relationship: buys, codecs, buy_codec

Tables

Schema::create('codecs', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('buys', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->string('name');
});   

Schema::create('buy_codec', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('buy_id')->unsigned();
    $table->foreign('buy_id')->references('id')->on('buys')->onDelete('cascade');

    $table->integer('codec_id')->unsigned();
    $table->foreign('codec_id')->references('id')->on('codecs')->onDelete('cascade');

    $table->timestamps();
}); 

This is my controller:

   class UserBuyController extends Controller
{
    public function create($userId)
    {
        $codecs = Codec::lists('name', 'id');
        $usr = User::findOrFail($userId);
        return view('buy.create', compact('usr', 'codecs'));
    }

    public function store($userId, Request $request)
    {
        $codecs = $request->input('codecs');
        $usr = User::findOrFail($userId)->buy()->create($request->except('codecs'));
        $usr->codec()->sync($codecs);
        return redirect('user/'.$userId.'/buy');
    }

    public function edit($userId, $id)
    {
        $codecs = Codec::lists('name', 'id');
        $buy = User::findOrFail($userId)->buy()->findOrFail($id);
        return view('buy.edit', compact('buy', 'codecs'));
    }
}

Create form

{!! Form::open(['method'=>'POST', 'action'=>['UserBuyController@store', $usr->id]]) !!}

    <div class="form-group">
        {!! Form::label('name', 'Name:') !!}
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-font"></i></span>
            {!! Form::text('name', null, ['class'=>'form-control']) !!}
        </div>
    </div>

    <div class="form-group">
        {!! Form::label('codecs', 'Outbound Codecs:') !!}
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-language"></i></span>
            {!! Form::select('codecs[]', $codecs, null, ['class'=>'form-control', 'multiple'=>true]) !!}
        </div>
    </div>

    {!! Form::submit('Submit', ['class'=>'btn btn-info']) !!}

{!! Form::close() !!}

And this is the edit form

  {!! Form::model($buy,['url'=>url('user/'.$buy->user->id.'/buy/'.$buy->id),'method'=>'patch']) !!}

    <div class="form-group">
        {!! Form::label('name', 'Name:') !!}
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-font"></i></span>
            {!! Form::text('name', null, ['class'=>'form-control']) !!}
        </div>
    </div>

    <div class="form-group">
        {!! Form::label('codecs', 'Outbound Codecs:') !!}
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-language"></i></span>
            {!! Form::select('codecs[]', $codecs, null, ['class'=>'form-control', 'multiple'=>true]) !!}
        </div>
    </div>

    {!! Form::submit('Update', ['class'=>'btn btn-info']) !!}

{!! Form::close() !!}

Model binding doesn't work

Something is wrong but I don't know what.

This is my pivot table. I have 2 codecs associated with buy_id 3

enter image description here

And this is my edit page.

enter image description here

Nothing is selected.


Update

Model

class Buy extends Model
{
    protected $guarded = ['id'];
    public function codec() {
        return $this->belongsToMany('App\Codec');
    }
    public function user() {
        return $this->belongsTo('App\User');
    }
}

class Codec extends Model
{
    protected $guarded = ['id'];
    public function buy() {
        return $this->belongsToMany('App\Buy');
    }
}
class User extends Authenticatable
{
    public function buy() {
        return $this->hasMany('App\Buy');
    }
}
Rwd

One solution would be to create an accessor for the codec ids and use that with Form::select() instead:

In your Buy model add the following accessor:

public function getCodecListAttribute()
{
    return $this->codecs->pluck('id')->toArray();
}

Then change you select block to:

<div class="form-group">
    {!! Form::label('codec_list', 'Outbound Codecs:') !!}
    <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-language"></i></span>
        {!! Form::select('codec_list[]', $codecs, null, ['class'=>'form-control', 'multiple'=>true]) !!}
    </div>
</div>

This will mean that when you try to get the value from the request you will have to use codec_list instead of codecs.

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel eloquent many to many to many model

From Dev

Laravel many-to-many relationship on the same model

From Dev

Form binding in laravel for one to many relationship

From Dev

Laravel 5 - access specific model on many to many relationship

From Dev

Laravel 5.1 Multiple Many to Many Relationship on the Same Model

From Dev

Laravel 5.2 Find a Model Based on a Many-to-Many relationship

From Dev

Laravel / Eloquent - Many to many pivot model with morphToMany relation

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Laravel Model Relation Where Many-to-Many Exists

From Dev

Laravel one to many model relationship not working

From Dev

User to User Model: One to Many Relationship on Laravel

From Dev

Laravel - Many to Many relationship

From Dev

Laravel - Many To Many

From Dev

Laravel Many to Many relation

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

From Dev

This is a many to many relationship model for question

From Dev

MPS Many to Many model transformation

From Dev

Iterating Through Many to Many Model

From Dev

Laravel 5 – get particular many to many relation based on model and related model ID

From Dev

Laravel 4: many to many (insertion)

From Dev

laravel many to many with third table

From Dev

Laravel Eloquent Many to Many Query

From Dev

laravel 5.1 many to many relation

From Dev

Laravel many to many selecting with Eloquent

From Dev

Laravel 6 many to many relationship

From Dev

Many to Many Relatioship Mysql and Laravel

From Dev

Laravel Many-to-Many Relation

Related Related

HotTag

Archive