![]() ![]() Otherwise, it would be necessary to use a try/catch block and the number of lines would be pretty much the same.Īs in the previous example, we have a conditional where we are calling $user->update() with the data that we want to update. To make the most out of this change, we need to know how to automate the handling of the exception thrown by the findOrFail(). This function has a special behavior where an exception gets thrown in case the data for the given ID didn't get found. ![]() I did create this method a few weeks back.Enter fullscreen mode Exit fullscreen modeĪt the first line, we are querying a user by its ID using the findOrFail() function. I am wondering if there is any built in L4 functionality for doing this in some way such as: $row = DB::table('table')->where('id', '=', $id)->first() ![]() $instance = static::firstOrNew($attributes) Public static function updateOrCreate(array $attributes, array $values = array()) * Create or update a record matching the attributes, and fill it with values. I found out a few weeks after writing this, that this is in fact part of Laravel's Eloquent's core.ĭigging into Eloquent’s equivalent method(s). Just in case people are still coming across this. Of course, don't forget to add DB facade: use Illuminate\Support\Facades\DB So for this specific case, just use the same function with your query builder like so: $matchThese = array('shopId'=>$theID,'metadataKey'=>2001) ĭB::table('shop_metas')::updateOrCreate($matchThese,) Query Builder works exactly the same as Eloquent so anything which is true for Eloquent is true for Query Builder as well. Here is reference for Query Builder from Laravel docs. Someone has asked if it is possible using Query Builder in Laravel. If no matching model exists, create one. One example from there is: // If there's a flight from Oakland to San Diego, set the price to $99. Otherwise it will throw error when executing above example: Illuminate\Database\QueryException with message 'SQLSTATE: General error: 1364 Field '.' doesn't have a default value (SQL: insert into `.` (`.`., `updated_at`, `created_at`) values (., xxxx-xx-xx xx:xx:xx, xxxx-xx-xx xx:xx:xx))'Īs there would be some field which will need value while inserting new row and it will not be possible, as either it's not defined in $fillable or it doesn't have a default value.įor more reference please see Laravel Documentation at: ShopId = $theID, metadateKey = 2001 and shopOwner = New OneĬheck your model for $fillable and make sure that you have every column name defined there which you want to insert or update and rest columns have either default value or its id column auto incremented one. If not found at all then it will insert a new row with: If it finds more than one matching rows then it will update the very first row that means which has lowest primary id. ShopMeta::updateOrCreate($matchThese,) Ībove code will check the table represented by ShopMeta, which will be most likely shop_metas unless not defined otherwise in the model itself.Īnd if it finds then it will update column shopOwner of found row to New One. As in Laravel >= 5.3, if someone is still curious how to do so in easy way it's possible by using: updateOrCreate().įor example for the asked question you can use something like: $matchThese =
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |