How to create and Update Laravel Eloquent

0 votes

What’s the best and quickest way to create a new record using Laravel Eloquent or update the record if it exists ?

Here is my snippet:

<?php

$user = User::where('organization_id', '=', $organization_id)
    ->where('age', '=', 22)->first();

if ($user == null) {
    // Insert a new record into the database
} else {
    // Update the existing record
}

Any help?

Nov 11, 2020 in Laravel by kartik
• 37,490 points
463 views

1 answer to this question.

0 votes

Hello @kartik,

There has been a couple of ways to create and update your Eloquent record separately. What you need here is one method that does all of this for you. You could use firstOrNew or firstOrCreate.

The difference is that :

  1. The first one only returns an instance of the model, meaning that you have the object, but it hasn’t been persisted into the database yet. You would have to call save() method on it. 
  2. The other one will do all of this for you:
$user = User::firstOrNew(['name' => 'Laravel Recipes']);
$user->age = Input::get('age');
$user->save();

Hope it helps!!

answered Nov 11, 2020 by Niroj
• 82,560 points

Related Questions In Laravel

0 votes
1 answer

How to update a pivot table using Eloquent in laravel 5?

Hello @kartik, The code below solved my problem: $messages ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,560 points
870 views
0 votes
1 answer

How to pass data through URL and access through controller in Laravel?

Hello, You can  first refer how to  Create controller through ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
4,950 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,560 points
188 views
0 votes
1 answer

Explain validations in laravel and How to validate my application incoming data?

Hey @kartik, In Programming validations are a handy ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
188 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
5,537 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
641 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
490 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
6,125 views
0 votes
1 answer

What is database migration and how to create database migration in Laravel?

Hii, Migrations are like version control for your database, ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
659 views
0 votes
1 answer

How to manually create a new empty Eloquent Collection in Laravel?

Hello @kartik, It's not really Eloquent, to add ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,560 points
1,405 views