Laravel 6 FirstorCreate Increment and update column using Eloquent

Today, We want to share with you Laravel 6 FirstorCreate Increment and update column using Eloquent.In this post we will show you wordpress plugin require another plugin, hear for eloquent update column laravel 6 we will give you demo and example for implement.In this post, we will learn about laravel updateorcreate increment with an example.

Laravel 6 FirstorCreate Increment and update column using Eloquent

There are the Following The simple About Laravel How to increment only when its a new insert? Full Information With Example and source code.

READ :  Angular Date filtering and formatting JSON Data

As I will cover this Post with live Working example to develop Eloquent: incrementing columns without update() function, so the Laravel eloquent update column value by adding new value is used for this example is following below.

Example 1: Laravel, Update Query and FirstorCreate Method
[php]
$product_id = 12058;
$url = “https://www.modi.com”;
$desc = “Nice and Good Producs for Web”;

Products::firstOrCreate([‘product_id’ => $product_id,’ext_time’=>date(“Y-m-d H:00:00”),’name’=> addslashes($product_name)],[‘up_votes’=>0,’web_domain’=>$url,’desc’=>$desc])->increment(‘up_votes’);

[/php]

very useful Laravel Methods firstOrCreate() to try and find a row, and create it if it doesn’t exist.

[php]
$tamilrokers = App\Tamil::firstOrCreate([‘name’ => ‘Tamil 10’]);

$tamilrokers = App\Tamil::firstOrCreate(
[‘name’ => ‘Tamil 10’], [‘pageview’ => 1]
);

$tamilrokers = App\Tamil::firstOrNew([‘name’ => ‘Tamil 10’]);

READ :  Angular ng controller directive Example

$tamilrokers = App\Tamil::firstOrNew(
[‘name’ => ‘Tamil 10’], [‘pageview’ => 1]
);
[/php]

[php]
function downVote($id)
{
DB::table(‘products’)
->where(‘id’, $id)
->decrement(‘upVotes’, 1)
->increment(‘downVotes’, 1);
}
[/php]

How to increment and update column in one eloquent query
[php]
$user = User::find($user->user_id);
if($user){
$user->increment(‘total_members’,1);
}else{
$user = new User();
$user->total_members = 1;
// Inert new user here with all related data
$user->save();
}
return $user->id;
[/php]

Creating and Update Laravel 6 Eloquent

[php]
$student = Student::firstOrNew(array(‘stud_name’ => Input::get(‘stud_name’)));
$student->age = Input::get(‘age’);
$student->save();
[/php]

useing updateOrCreate
[php]
$student = App\Student::updateOrCreate(
[‘lastname’ => ‘chovatiya’, ‘destination’ => ‘surat’],
[‘age’ => 38]
);
[/php]

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

READ :  Eloquent Dynamic Table name using Laravel 6

I hope you get an idea about laravel 6 update method.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment