Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Properly Update Pivot table using added column as query?

I have this pivot table containing

imei status item_id

and here is my code

  $update = Item::find($item_id)->workArea()->wherePivot('imei', '=', 3)->first(); 
  $update->pivot->status = 'Sold';
  $update->pivot->save();

Before the function execute:
IMEI     STATUS     ITEM-ID
3        Available     3
5        Available     3

Result:
IMEI     STATUS     ITEM-ID
3        Sold          3
3        Sold          3

What I need:
IMEI     STATUS     ITEM-ID
3        Sold          3
5        Available     3

Here's my relationship

public function workArea()
    {
       return $this->belongsToMany('WorkArea','item_work-area','item_id','work-area_id')->withPivot('imei','status');
    }

here are the past conversation with other artisan http://laravel.io/forum/07-08-2014-proper-chaining-for-my-pivot-table-to-execute-pivot-update

like image 245
Martney Acha Avatar asked Feb 03 '26 13:02

Martney Acha


1 Answers

This is in bugged with the current version 4.1

What I do is make a raw query instead of eloquent ORM,

 DB::table('item_work-area')
        ->where('imei', 11)
        ->update(array('status' => 'Sold'));

Hope this help for those who need to update their pivot table using added column as reference.

like image 65
Martney Acha Avatar answered Feb 05 '26 03:02

Martney Acha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!