7

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();
});    

Models

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

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

class User extends Authenticatable
{
    public function buy() {
        return $this->hasMany('App\Buy');
    }
}

I want to populate the buy table with the user_id, and to attach the codecs from the codecs table.

This is the buy 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() !!}

Everything works fine, if i don't attach the codecs.

UserBuyController

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)
    {
        $usr = User::findOrFail($userId)->buy()->create($request->all());
        return view('buy.index');
    }
}

How can I create the buy record(because i need the buy id) and than attach the codecs in the pivot table?

Thanks!

3 Answers 3

9

In model, You have to mention the intermediate table.

class Codec extends Model
{
    protected $guarded = ['id'];
    public function buy() {
        return $this->belongsToMany('App\Buy', 'buy_codec', 'codec_id', 'buy_id');
    }
}

and

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

And then in controller,

public function store($userId, Request $request)
{
        $buy = User::findOrFail($userId)->buy()->create($request->all());
        $buy->codec()->attach([codec_ids]);
        return view('buy.index');
}

You can attach more codec objects or ids using attach method.

Please refer this link

https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models

Sign up to request clarification or add additional context in comments.

2 Comments

How can i do de model binding now?
no need to mention to intermediary table if the naming convention is as rules
4

You can attach() or sync(). I always store pivot data with attach() function.

    $buy = Buy::findOrFail($id);
    $buyId = $request->input('codec');
    $buy->codec()->attach($buyId);

Comments

1

There are few ways you can achieve this. You can attach() or sync(). I always store pivot data with attach() function.

$usr = User::findOrFail($userId)->create($request->all());
$usr->buy()->attach($request->codecs);

or, you can inline the query:

$usr = User::findOrFail($userId)->create($request->all())->buy()->attach($request->codecs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.