Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Saving to pivot table in many to many relationship with extra column

Tags:

php

laravel

I have models Transaction and Option, that have many to many relationship. The pivot table transaction_options has columns transaction_id, option_id and price. I have defined the relationships in the models like this:

Transaction model:

public function options()
{
    return $this->belongsToMany('App\Option', 'transaction_options')->withPivot('price');
}

Option model:

public function transactions()
{
    return $this->belongsToMany('App\Transaction', 'transaction_options')->withPivot('price');
}

I am wondering how can I save something in the pivot table, I have tried something like this, but it is not working:

   foreach($data->options as $option) {
        $transaction->options()->create([
            'transaction_id' => $transaction->id,
            'extras_id'      => $option->extraId,
            'option_id'      => $option->id,
            'price'          => $option->price
        ]);
    }

How can I do this?

like image 708
Leff Avatar asked Jan 18 '18 14:01

Leff


People also ask

How do I save a pivot table in Laravel?

Route::get('/', function () { $user = \App\User::find(2); $job = \App\Job::with('notifications')->find(3); $job->notifications->is_accepted = 'Y'; // is_accepted is pivot table's column $notification = $user->notifications()->save($job); });

How do you make a many to many relationship in Laravel?

Many to Many relations is one of the eloquent relationships in Laravel. The main factor in many many relations is the to join table that is called the pivot table. The pivot table has the ability to perform relations from one model to many models.

What is pivot attribute in Laravel?

In laravel, When working in a many-many relationship, the user has to choose the intermediate table and it is called a pivot table in terms of Laravel. The pivot attribute communicates with this intermediate table in controllers or models in Laravel. The admin in laravel has to define the role of every other user.


3 Answers

Pass the extra field as a value to attach function

$transaction->options()->attach([$request['id'] => ['price' => $request['price']]]);
like image 96
Mahdi Younesi Avatar answered Oct 17 '22 19:10

Mahdi Younesi


https://laravel.com/docs/master/eloquent-relationships#updating-many-to-many-relationships

From what I can see, you should use your code like this:

foreach ($data->options as $option) {
    $transaction->options()->attach($option->id, ['price' => $option->price])
}
like image 34
Eternal1 Avatar answered Oct 17 '22 18:10

Eternal1


You can pass pivot data as the second parameter:

foreach($data->options as $option) {
    $transaction->options()->create(
        ['extras_id' => $option->extraId],
        ['price' => $option->price]
    );
}

You also don't need to pass both transaction_id and option_id, they will be inserted automatically.

like image 37
Alexey Mezenin Avatar answered Oct 17 '22 17:10

Alexey Mezenin