
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
- Symlink(): No such file or directory
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to add a key value pair to existing array in laravel
- Remove array keys and values if it does not exist in other array in Laravel
- How to remove P tag from CkEditor in Laravel?
- How to get last record from object collection in laravel
- Datetime field in Laravel migration
- How to pass link from controller to view in laravel on ajax call
- Run artisan command to generate key in laravel
- How to get list of all views file in laravel
- Cannot end a section without first starting one
- Create record with unique slug in laravel
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- How to get tomorrow and yesterday date in laravel
- Update email with unique validation in laravel
- How to pass data to partial view file in laravel
- How to get all route list
- How to get only time from created_at in laravel
- How to create new user without form submission in laravel
- Get current month records in laravel 7/8
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Ajax GET request in laravel
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- Call to undefined method App\Models\User::follow()
- How to send email in laravel