Try this:
$update=Item::find($item_id);
$update_imei=$update->workArea()->where('imei','=','3')->get();
$update_imei->pivot->status = 'Sold';
$update_imei->pivot->save();
Error - "Creating default object from empty value "
What line? What part of the code is throwing this? Are you able to access the pivot tables values at all? I can't really tell what is going on just by that error. Does workArea() return a collection or an instance?
its the line $update_imei->pivot->status = 'Sold'; - "Creating default object from empty value "
Yes Im able to access those pivot table values, yes workArea returns collections
Then instead of calling ->get(); use ->first(); unless you want to update more than one items pivot table, then you need a foreach statement.
It works now, But the issue now is when it updates the pivot table, it will also update other imei that has the same Item_ID.
Sorry, I'm confused. I can't tell what you are referring to. Are you speaking of the item_id that is used to find the item, or are you referring to the ->where() queries item id of '3'? Is it updating another pivot table other than the one we are accessing?
Another row is updated other than what we access,
What we want to access is Imei=3
IMEI Item_id Status 6 2 Available 3 1 Available 5 1 Available
But what happens after the query is, it updates same item_id rather than IMEI only.
Result that we need IMEI Item_id Status 6 2 Available 3 1 Sold 5 1 Available
Issue IMEI Item_id Status 6 2 Available 3 1 Sold 3 1 Sold
it converts all item_id to same IMEI 3
Can you paste your code in laravel.io/bin. Your last post just confused me even more. I don't understand the numbers you have posted. - "Status 6 2 Available 3 1 Sold 3 1 Sold".
Also, I just noticed that you are passing in '3' as a string and not an int. $update_imei=$update->workArea()->where('imei', '=', 3)->first();
It should only be updating the first imei's,that equals 3, pivot table.
Martney, all the data in one table?
Also, Model::find() gets a record by it's primary key, and primary keys are suppose to be unique. If all of that data is in the same table then you can use this bit of code
$update=Item::where('item_id', '=', $item_id)->where('imei', '=', 3)->first();
$update->status = 'Sold';
$update->save();
Otherwise you will need to specify a primary key for your items table. Then you can user Model::find() to select the item from the database. I suspect this is the issue. While I still don't know why the imei is being updated since we are only calling update on the pivot table which makes me want to see how you have your relationships set up. Does workArea() do anything other than return the relation?
its pivot table, yes IMEI and item_id is in my pivot table, I have my item table and a pivot table work it http://laravel.io/bin/lnVR1 here's the code for my relationship
$update = Item::find($item_id)->workArea()->wherePivot('imei', '=', 3)->first();
$update->pivot->status = 'Sold';
$udpate->pivot->save();
updated
Hi CleverCookie same error occur as for my first issue,
Call to undefined method Illuminate\Database\Query\Builder::updateExistingPivot()
Sorry, I should have realized sooner that we were using the where query on the WorkArea's table and not the actual pivot table. i updated my answer to use 'wherePivot()' method I just found when searching. Review the updated code.
I don't know why it is updating two pivot tables and updating a value that we are not specifying.
Does $update->pivot return two items in a collection when you var_dump()?
No it does not update two pivot table, but it updates 2 column of 1 pivot table.
I think this is a bug from laravel.
Yea, two pivot table columns is what I meant.
It does sound like a bug. Hopefully it is not something simple that we are missing.
I guess you should report it and maybe we can get some answers because I'd like to know why it's not working as well.
Yeah hopefully were not haha
Ive been debugging this issue for almost 2 days now
where should I report this issue ?
Well, either way I learned that I can use a query restriction on pivot tables so it's been fun. Now, I just need to know how to do it correctly. lol
Report Issues on github: https://github.com/laravel/framework/issues
I already reported this issue and apparently same as issue as this https://github.com/laravel/framework/issues/3215
What I do is a raw query cause it is still currently bug
DB::table('item_work-area')
->where('imei', 11)
->update(array('status' => 'Sold'));
I experienced something similar and filed an issue Github.
Basically from what I understand it seems that the ->where()
gets ignored when using ->updateExistingPivot()
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community