gpt4 book ai didi

php - 如何检查是否在 laravel Eloquent 中插入了一行表?

转载 作者:行者123 更新时间:2023-11-29 04:38:07 26 4
gpt4 key购买 nike

在从数据库查询所有信息之前,我想检查表的一行是否被插入,如果没有任何新的插入,我不会使用查询方法。但是我很了解如何检查是否在带有 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
}
]
}

最佳答案

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

在 Controller 中:

     $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
}

关于php - 如何检查是否在 laravel Eloquent 中插入了一行表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36440662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com