繁体   English   中英

如何检查表行是否雄辩地插入了laravel?

[英]How do I check if a row of a table was insert in laravel eloquent?

在查询数据库中的所有信息之前,我想检查表的行是否已插入以及是否没有任何新的插入,我将不使用查询方法,但是我对如何检查是否已插入非常了解。用雄辩的库插入或不插入Laravel5中。

我研究了一些我在下面的sql语句中找到的教程,但是我没有在Eloquent中使用它,而这个sql是否按我的预期工作。

关于checkingIfNotificationUpdated方法,我只想在将行插入到表中时检查真假,但是这句话将查询所有数据

   public function checkingIfNotificationUpdated(){

//       SELECT * FROM INFORMATION_SCHEMA.'.$this->table.' WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < UPDATE_TIME
         RETURN self::select('*')->where(DB::raw('DATE_SUB(NOW(), INTERVAL 1 HOUR)'),'<','UPADTE_TIME')->get();
    }

这是我检查通知表是否插入的查询方法

  public function getNotification($user_id, $gId)
    {
        if($this->checkingIfNotificationInserted()  == true){
            $this->_data = self::select('*')->join('users', 'users.id', '=', 'n_user_id')
                ->where('n_group_code_id','=', $gId)
                ->get();
            if (count($this->_data)) {
                return $this->_data;
            } else {
                return false;
            }
        }else{
            // to go with old data.
        }

    }

这是使用dd()时我的响应数据

Collection {#619 ▼
  #items: array:1 [▼
    0 => Notification {#620 ▼
      #table: "notification"
      +timestamps: true
      -_data: false
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:11 [▼
        "n_id" => 269
        "n_user_id" => 69
        "n_group_code_id" => 11
        "n_source_id" => 231
        "n_activity_type" => "Issue Till"
        "n_create_times" => "2016-04-06 09:04:40"
        "n_description" => "Issue Till"
        "n_status" => 0
        "url" => "teller/trans_issues"
        "updated_at" => "2016-04-06 09:45:40"
        "created_at" => "2016-04-06 09:45:40"
      ]
      #original: array:11 [▼
        "n_id" => 269
        "n_user_id" => 69
        "n_group_code_id" => 11
        "n_source_id" => 231
        "n_activity_type" => "Issue Till"
        "n_create_times" => "2016-04-06 09:04:40"
        "n_description" => "Issue Till"
        "n_status" => 0
        "url" => "teller/trans_issues"
        "updated_at" => "2016-04-06 09:45:40"
        "created_at" => "2016-04-06 09:45:40"
      ]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #guarded: array:1 [▼
        0 => "*"
      ]
      #dates: []
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
    }
  ]
}

如果您使用的是Eloquent,则无需查询回数据库。

假设您这样插入:

$myModel = ModelClass::create($params);

或这个:

$myModel = new ModelClass($params);
$myModel->save();

然后,您可以执行以下检查:

if (! empty($myModel->id)) {
    // Model has been successfully inserted
}

我认为您可以在插入时检查输入字段:在这里,

在控制器中:

     $input = $request->all();

    //check input value if exists or not in db...

     $b_exists = ModelClass::where('title','=',$input['title'])->exists();

     if($b_exists){
     //show message: alteady exists
     }
     else{
     //
     ....new data inserted 
    }

暂无
暂无

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

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