Support the ongoing development of Laravel.io →
Input Eloquent
Last updated 2 years ago.
0

Try this:

$update=Item::find($item_id); 
$update_imei=$update->workArea()->where('imei','=','3')->get(); 
$update_imei->pivot->status = 'Sold';
$update_imei->pivot->save();
Last updated 2 years ago.
0

Error - "Creating default object from empty value "

Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Then instead of calling ->get(); use ->first(); unless you want to update more than one items pivot table, then you need a foreach statement.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

CleverCookie here you go http://laravel.io/bin/qk2RO

Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0
$update = Item::find($item_id)->workArea()->wherePivot('imei', '=', 3)->first(); 
$update->pivot->status = 'Sold';
$udpate->pivot->save();

updated

Last updated 2 years ago.
0

Hi CleverCookie same error occur as for my first issue,

Call to undefined method Illuminate\Database\Query\Builder::updateExistingPivot()

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Attached here is the issue

http://laravel.io/bin/Noqae

I'm currently debugging it.

Last updated 2 years ago.
0

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()?

Last updated 2 years ago.
0

No it does not update two pivot table, but it updates 2 column of 1 pivot table.

  • It only return 1 item which is right for the IMEI we specified.

I think this is a bug from laravel.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Yeah hopefully were not haha

Ive been debugging this issue for almost 2 days now

where should I report this issue ?

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

I already reported this issue and apparently same as issue as this https://github.com/laravel/framework/issues/3215

Last updated 2 years ago.
0

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'));
Last updated 2 years ago.
0

I experienced something similar and filed an issue Github.

Basically from what I understand it seems that the ->where() gets ignored when using ->updateExistingPivot()

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Martney martney Joined 8 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.