
Create a record if not exist in laravel
Create a record if not exist in laravel
In this session, we are going to try to solve the "Create a record if not exist in laravel" puzzle by using the computer language.
-
Laravel firstOrCreate method to create a record if it not already exist in the table
--PATH app\Http\Controllers\<YourController>.phpPost::firstOrCreate( ['slug'=> $request->slug], ['title' => $request->title, 'body' => $request->body] );
firstOrCreate method is used to create a record if it does not already exist in the table in laravel. It has two parameters, conditions and fields and these parameters should be an array. It tries to find a model matching the attributes you pass in the first parameter. If a model is not found, it automatically creates and saves a new record after applying attributes passed in the second parameters including first parameter field and values.
This code snippet will create a new post record including title, body and slug fields in the post table if the record is not already exist with this particular slug.
-
Create a new record if not exist in laravel using create method
--PATH app\Http\Controllers\<YourController>.php$user = User::where('username', $request->username)->first(); if(!$user){ $user = User::create([ 'email' => $request->email, 'username' => $request->username, 'password' => bcrypt($request->password) ]); }
This code snippet helps you to find the record if any already exists with this particular username and check that if the user with this particular username is not already exist then it creates a new user using the create() method.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Update record after find method in lavavel
- Get content from web URL in laravel
- How to call model in blade laravel
- Route [password.request] not defined
- Ajax GET request in laravel
- How to get last year records count with month wise in Laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Get last week data in Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Get last year created records in Laravel
- How to get all posts which contains comments in laravel
- Multiple Level eager loading in Laravel
- Split an Eloquent Collection by half in Laravel
- Generate random string lowercase in Laravel
- Laravel clone model
- How to check find method executed successfully in laravel
- Use withCount() to get total number of records with relationship
- How to increment column value of table in Laravel
- How to delete record in Laravel with ajax
- If condition in foreach loop in laravel
- How to Access Array in blade laravel
- Get only 10 records from table in laravel
- Remove several global scope from query
- How to add script on specific view file in laravel while extending layout
- Delete file from amazon s3 bucket using Laravel