It is more explicit as update returns the same 0 for a non-existent user and a if the user details already where what you are trying to set it to. So for you it might be better to first check if the user exists with either a User::findOrFail($id) or just check the return of User::find($id) and then if the user exists you do your conditional update on the returned $user. The ->update() method on a query builder will return an int, you can. To update a model, you may retrieve it, change an attribute, and use the save. The ->save() method to a model will return true even if no changes to the model were made. Run the same query again and you will notice it returns 0 affected rows as the name is already Testing > $retval = DB::table('users')->where('id', 1)->update() The inserted model instance will be returned to you from the method. For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent. MySQL unfortunately doesn't have such support on a statement level. Accessors, mutators, and attribute casting allow you to transform Eloquent attribute values when you retrieve or set them on model instances. If you care about performance more than using Eloquent then with raw queries in PostgreSQL (with RETURNING clause) or SQL Server (with OUTPUT clause) you can return updated records in one go. > $retval = DB::table('users')->where('id', 1)->update() IMHO your way is arguably the most efficient with Eloquent. Eloquent Update Method return model Issue 13488 laravel/framework GitHub. Useful side note, play with php artisan tinker, ie. You could spend weeks binging, and still not get through all the content we have to offer. Think of Laracasts sort of like Netflix, but for developers. So if you try to update the name and lastname of a user with the details it actually already contains the query will return 0. A massive community of programmers just like you. However, for you it might also not signify an error even if a 0 was returned as it just means that no records where updated. The methods first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the. Whether you’re new to Laravel or an experienced developer looking to level up your Eloquent skills, this list will equip you with the knowledge of key features that will streamline your database. 0 means that no records have been updated. So the above will return the number of records that where updated. An update query returns the number of rows that where affected by the update query.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |