简体   繁体   中英

check if the row exists or not in the database

hi i'm inserting some rows in my database , and i want to check if the value exists then don't insert , if not then insert it , that's my controller :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Auth;
use App\User;
use App\Product;
use DB;

class listController extends Controller
{
    //
    public function getIndex(Request $request){
        $user_id=Auth::user()->id;
        $list=DB::table('wishlist')
                ->join('products','wishlist.product_id','=','products.id')
                ->join('users','wishlist.user_id','=','users.id')
                ->select('wishlist.id','wishlist.product_id','wishlist.user_id',
                    'products.id as p_id','products.name','products.salary','products.image_name',
                    'users.id as u_id')
                ->where('wishlist.user_id','=',$user_id)
                ->get();

        return view('contents.wishlist')->with('list',$list);
    }
    public function postIndex(Request $request){
        $id=$request->input('id');
        $user_id=Auth::user()->id;
        $product_id =$request['product_id'];


        DB::table('wishlist')->insert(['id'=>$id,'user_id'=>$user_id,'product_id'=>$product_id]);
        return redirect('wishlist');
    }
    public function deleteDeleteProduct(Request $request,$id){
        $pro=$request->get('id');
        DB::table('wishlist')->where('id',$pro)->delete();
        return redirect('wishlist');
    }
}

can anyone help me checking if it exists or not ?

the answer was to use the count function like that :

public function postIndex(Request $request){

        $id=$request->input('id');
        $user_id=Auth::user()->id;
        $product_id =$request['product_id'];

        $wishlist=DB::table('wishlist')
                    ->where('user_id','=',$user_id)
                    ->where('product_id','=',$product_id)
                    ->count();

        if($wishlist > 0){
            return redirect('wishlist');
        }
        else{
            DB::table('wishlist')
              ->insert(['id'=>$id,'user_id'=>$user_id,'product_id'=>$product_id]);
            return redirect('wishlist');
        }
}

If you are using mysql you can make use of INSERT IGNORE and laravel's raw queries

The only condition is that you have an UNIQUE index defined on a column in the table.

DB::insert(
     'insert ignore into wishlist (id, user_id, product_id) values (?, ?)', 
     [$id, $user_id, $product_id]
);

Basically, you can run a query from the wishlist table searching for that particular product. After that, if the query has no result, you insert the data. For example,

$found = DB::table('wishlist')->where('name', $product_name)->first();
if (!$found) {
   //insert to table
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM