Stats: 3,191,145 members, 7,943,111 topics. Date: Sunday, 08 September 2024 at 06:09 AM |
Nairaland Forum / Science/Technology / Programming / Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() (9527 Views)
Please I Get This Error When Submit Form (notice: Undefined Index) / DHTMLSQL - A Very Advanced Wrapper For Mysql Database! / Common Error In Laravel #1:solved File_put_contents Failed To Open Stream (2) (3) (4)
Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by dplumptre(m): 3:06pm On Aug 28, 2015 |
Hi Guys, In case some of you have experence this error that says "Call to undefined method Illuminate\Database\Eloquent\Collection::save()" when using eloquent in laravel here is the solution, firstly a typical update statement in laravel using eloquent may look like this below $account = Account::find(10); The code above means that there is a table in the database represented by "Account::" so you are trying to update the field called name where the id is 10 , which should work perfectly but there are times where you might not have the id but perhaps there is a field called gender so you might want to do something like this below but this will give you and error
looking at the code above, its trying to update the name where the gender is male ... you will get an error that says Call to undefined method Illuminate\Database\Eloquent\Collection::save() Basically the problem is that it will retrieve all rows from that model (filtered by where condition ). So you are getting a Collection instead of an instance of the model. so what you need to do is to make sure you get just an instance by using first , view the code below
if you want to see more stuffs visit http://overallheuristic.com/blog/ site : http://overallheuristic.com twitter : @dplumptre fb :https://www.facebook.com/
|
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by maekhel(m): 3:10pm On Aug 28, 2015 |
Nice one bro 1 Like |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by jidez007: 4:01pm On Aug 28, 2015 |
OK no problem. But can you please change that $user->save() to $account->save(). You can also do this Account::find(10)->update(['name' => 'new name']); but don't forget the mass assignment settings in the model. |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by maekhel(m): 4:04pm On Aug 28, 2015 |
Guys, I need your help in another thread https://www.nairaland.com/2559716/populating-select-menu-model-user |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by dplumptre(m): 1:48am On Aug 29, 2015 |
@jidez007 thanks av updated the code , leaving it that way might just bring confusion to peeps. |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by jacob05(m): 10:09pm On Aug 29, 2015 |
All multi-result sets returned by Eloquent, either via the get method or a relationship, will return a collection object. Simple put... If the result of your query is likely to return more than one value, Laravel returns a collection. Understanding query methods that returns a Collection would save you the problem of calling an Eloquent model method on a Collection.. |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by jacob05(m): 10:20pm On Aug 29, 2015 |
In the Above, We're trying to find an Account in the database that has an id of 10 .... so this query is always going to return a single Object or none from the database. so an Eloquent Model object of Account would be returned from the database if there is an Account with an id of 10 in the database. With the returned Eloquent model, we can update the name field and call the save method directly. |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by jacob05(m): 10:56pm On Aug 29, 2015 |
In the Above, We're trying to find all Accounts in the database that has the value of 'male' as gender or that is owned by a male .... so this query is most likely to return more than one record from the database or none from the database. This would cause 'Eloquent' to return a Collection even if the number of records found is One/1.. Because the the returned value is a Collection, we would need to loop through the returned set to have access to the individual Model. In the above, the value returned to $account is a Collection. so trying to access a property or an Eloquent method on the result would cause an error... The above can be rewritten as We're now changing the name of all male account holders to "Ademola Plumptre"; If we only want to change the name of the first Account in the DB that is owned by a male then we chain our where with first method... Note ... the result of first can only be one record, so Eloquent returns an Eloquent model of the first Account which we can modify after fetched..
|
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by Urine: 12:22pm On Aug 30, 2015 |
Laravel folks are taking over! |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by Nobody: 2:45pm On Aug 30, 2015 |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by Urine: 2:46pm On Aug 30, 2015 |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by jacob05(m): 3:30pm On Aug 30, 2015 |
pcguru1: Ha ha.... why are you MEAN na... lol. |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by Nobody: 4:14pm On Aug 30, 2015 |
Don't mind me jor, you Laravel guys get all the tools and nice concepts as in so many awesome things i've seen in Laravel but too much learning curve to jump ship from Yii2 to L5. OctoberCMS is something I wish was on Yii2 it's just cool 1 Like |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by Urine: 9:40pm On Aug 30, 2015 |
pcguru1: Jump ship, the level of support is crazy. |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by dplumptre(m): 12:31pm On Sep 02, 2015 |
The code above works by @jacob05 but you can also do it this way below , there are different ways really, its always good to just pick the one , you are comfortable with.
Its good to know we have laravel peeps in the house I will try to keep the post coming and hopefully we will all learn a thing or two from ideas we share together . if you want to see more stuffs visit my blog http://overallheuristic.com/blog/ site : http://overallheuristic.com twitter : @dplumptre fb :https://www.facebook.com/ |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by jidez007: 4:53pm On Sep 13, 2015 |
dplumptre: you guys wan kill una database abi. Imagine having 10,000 results from that collection. The total queries that will be executed from the database will be 10,0001. Because you are looping through and updating one by one. you should rather do Account::where('gender','=','male')->update(['name' => "Ademola Plumptre"]); just 1 query. 1 Like |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by osazekid(m): 11:09am On Sep 15, 2015 |
Any ebook you recommend for learning laravel5 |
Re: Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() by maekhel(m): 12:37pm On Sep 15, 2015 |
osazekid:https://leanpub.com/easylaravel http://learninglaravel.net/laravelbook hope u ready to spare some bucks sha! |
(1) (Reply)
Is 5 Months Enough To Learn Java? / Is Anything Better Than The Netbeans Ide? / How To Generate Unique 10/11 Digits Nuban / BVN With PHP
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 27 |