简体   繁体   English

检查数据库中是否存在该行

[英]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 : 答案是这样使用count函数:

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 如果您使用的是mysql ,则可以使用INSERT IGNORElaravel的原始查询

The only condition is that you have an UNIQUE index defined on a column in the table. 唯一的条件是您在表的列上定义了UNIQUE索引。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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