laravel通过数据库检查和插入,没有重复

时间:2018-04-02 13:44:15

标签: php database laravel

我已经解决了我的一个问题

  1. 首先是用户无法添加自己的数据(已解决)
  2. 将他的部门成员添加到同一文件/文档上一次。
  3. 不应该使用相同的用户再次添加
  4. 当我再次向用户分享文件时,它再次添​​加我确定有一种方法可以验证。事情是我在这个框架上是新手。我在谷歌上找到的东西并没有帮助我。

         //getting the initial request 
            $cruds_initials = DB::table('cruds')->where('id',$id)->first();
    
            $cruds_initials->document;
            $cruds_initials->year_release;
            $cruds_initials->item_code;
            $cruds_initials->file;
            $cruds_initials->user_id;
            $cruds_initials->sharetoken;
            $cruds_initials->privacy;
            $cruds_initials->document_owner;
            $cruds_initials->created_at;
            $cruds_initials->updated_at;
    
            //checking the tables if the initial request has values
            $cruds = DB::table('cruds')->where('id', $id)->get();
            $user_name = $request->Division;
            $users = DB::table('users')->where('division' ,$user_name)->get();
    
            //looping through database that has users + cruds database
    foreach($users as $key){
        foreach($cruds as $cey){
    
    
            $user_id = $key->id;
            $cruds_id = $key->id;
            $cruds_owner = $cey->document_owner;
            $cruds_division = $cey->division;
            $cruds_document = $cey->document;
            $cruds_year_release = $cey->year_release;
            $cruds_sharetoken = $cey->sharetoken;
            $cruds_privacy = $cey->privacy;
            $cruds_item_code = $cey->item_code;
            $cruds_file = $cey->file;
            $cruds_created_at = $cey->created_at;
            $cruds_updated_at = $cey->updated_at;
    
        // 1 do not add the users that will share the file
           if(Auth::id() == $user_id){
                echo "don't insert my id <br>";
                continue;
    
           }else{  
       // 2 to add the users who dont have the files 
       // 3 do not add the users who already have the files
                    if($user_id <>  $cruds_owner ){
                        $result=DB::table('cruds')->insert([
    
                        'division'         => $cruds_division, 
                        'document'         => $cruds_document,
                        'year_release'     => $cruds_year_release,
                        'item_code'        => $cruds_item_code,
                        'file'             => $cruds_file,
                        'user_id'          => $user_id,     //share to this user
                        'sharetoken'       => $cruds_sharetoken,
                        'privacy'          => $cruds_privacy,
                        'document_owner'   => $cruds_owner,   //user who owns the file
                        'created_at'       => $cruds_created_at,
                        'updated_at'       => $cruds_updated_at
    
                        ]);
                        continue;
    
                    }else{
                        echo "<hr>not insert";
                    } 
                }
                continue;
        }
    }
    

0 个答案:

没有答案