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
- How to prevent host header attack in Laravel
- How to pass data to multiple partial view files in laravel
- How to authenticate admin users in Laravel ?
- How to insert ckeditor data into database in Laravel?
- Sample configuration files to create laravel project with docker using wsl (window subsystem linux)
- Sample .htaccess file and index.php file under public directory in laravel
- How to pass variable from controller to model in Laravel
- How to get list of all views file in laravel
- Convert multidimensional array to single array in Laravel
- Rendering HTML from database table to view in Laravel
- FirstOrCreate() Not Inserting Model
- How to upload image in laravel 8
- How to make Copy or Duplicate table row in laravel
- Route not defined in Laravel
- Get previous date data in laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- How to create static page in Laravel
- How to run a specific seeder class in laravel
- How to change default timestamp fields name in Laravel
- How to display serial number in Laravel?
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- How to restore multiple records after soft-deletes in Laravel
- Where to use whereNotNull eloquent in laravel
- Send id with route Laravel
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'