Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a pivot table using Eloquent in laravel 5

I am new to laravel. I am working on a laravel 5 app and I am stuck here. I have 2 models as such:

class Message extends Eloquent{

    public function user()
    {
        return $this->belongsTo('App\User', 'from');
    }

    public function users()
    {
        return $this->belongsToMany('App\User')->withPivot('status');
    }
}

class User extends Eloquent {

    public function messages()
    {
        return $this->hasMany('App\Message', 'from');
    }

    public function receive_messages() {
        return $this->belongsToMany('App\Message')->withPivot('status');
    }
}

There exist a many-to-many relationship between Message and User giving me a pivot table as such:

Table Name: message_user
Colums:
message_id
user_id
status

I have an SQL query as such:

update message_user
set status = 1
where user_id = 4 and message_id in (select id from messages where message_id = 123)

How can I translate this query to the laravel equivalent?

like image 584
Fokwa Best Avatar asked Nov 05 '15 11:11

Fokwa Best


People also ask

How do I update a pivot table in Laravel?

There are many ways to update the pivot table in Laravel. We can use attach(), detach(), sync(), and pivot attribute to update the intermediate table in Laravel.

How do you refresh a pivot table?

Tip: You can also refresh the PivotTable by right-clicking on the PivotTable, and then selecting Refresh. To update all PivotTables in your workbook at once, click Analyze > Refresh arrow > Refresh All.

What is pivot table in Laravel?

Definition of Laravel Pivot Table. A pivot table is defined as the set of values arranged in a table form where every distinct value of the concerned table can be accessed in the form of a spreadsheet, database, and so on. It is available in one or multiple discrete functionalities.


2 Answers

The code below solved my problem:

$messages  = Message::where('message_id', $id)->get();
foreach($messages as $message)
   $message->users()->updateExistingPivot($user, array('status' => 1), false);
like image 101
Fokwa Best Avatar answered Oct 18 '22 21:10

Fokwa Best


You may use one of these two functions, sync() attach() and the difference in a nutshell is that Sync will get array as its first argument and sync it with pivot table (remove and add the passed keys in your array) which means if you got 3,2,1 as valued within your junction table, and passed sync with values of, 3,4,2, sync automatically will remove value 1 and add the value 4 for you. where Attach will take single ID value

The GIST: if you want to add extra values to your junction table, pass it as the second argument to sync() like so:

$message = Messages::find(123);
$user = User::find(4);

// using attach() for single message
$user->message()->attach($message->id, [
    'status' => 1
]);



$message2 = Messages::find(456); // for testing

// using sync() for multiple messages
$user->message()->sync([
    $message->id => [
        'status' => 1
    ],
    $message2->id => [
        'status' => 1
    ],
]);
like image 20
Mo Kawsara Avatar answered Oct 18 '22 22:10

Mo Kawsara