PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, April 18, 2022

[FIXED] how to use condition if using DB::table

 April 18, 2022     eloquent, laravel, laravel-7     No comments   

Issue

I'm trying to update or insert base on condition, but I'm getting error this condition not working. If it notification_status DELETED it was updated table and also it create new record at same time

code

use App\Notification;


  if(DB::table('notifications')->where('user_id',$request->user_id)->where('notification_status', '=', 'ACTIVE' )){
 DB::table('notifications')->update(['user_name' => $request->user_name,]);//if notification_status ACTIVE IT GET UPDATE
    }else{
 DB::table('notifications')->insert(['user_name' => $request->user_name,]);//if notification_status DELETED CREATE NEW RECORD
    }

Solution

use updateOrInsert so you can avoid if condition

 DB::table('notifications')->updateOrInsert(['user_id'=>$request->user_id,'notification_status'=> 'ACTIVE'],['user_name' => $request->user_name])


Answered By - John Lobo
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing