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 display user profile after login in laravel
- Insert dummy data in users table Laravel
- Datetime field in Laravel migration
- Extract only time from datetime in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- How to validate URL with https using regex in laravel
- How to remove P tag from CkEditor in Laravel?
- FirstOrCreate() Not Inserting Model
- How to get the id of last record from collection object in laravel view
- Show old value while editing the form in Laravel
- Laravel 5.4 save data to database
- How to print form data in laravel
- Send post data from controller to view
- How to get route method name in Laravel
- Get last week data in Laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- How to get path from current URL in Laravel
- How to increment column value of table in Laravel
- Insert current date time in a column using Laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Laravel 7 login error message not showing
- How to display HTML tags In Laravel blade