2016-04-06 29 views
0

在查詢來自數據庫的所有信息之前,我想檢查一行表是否被插入,如果沒有任何新的插入,我將不使用查詢方法。但我很瞭解如何檢查它被插入或不插入Laravel5和Eloquent庫中。如何檢查表格的某一行是否插入laravel雄辯?

我研究了一些教程,我發現如下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 
    } 
    ] 
} 

回答

1

我認爲你可以檢查你的輸入字段: 在這裏,

在控制器:

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

我這麼認爲,但還沒有成爲我的擔憂 –

1

如果您使用Eloquent,則無需查詢回數據庫。

假設你做這樣的插入:

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

或本:

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

然後,您可以做此項檢查:

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

我的方法不插一句它將被另一個函數或控制器我只是想檢查時通知表是由另一個人插入或沒有如果,所以我會查詢做好相關數據到客戶端。並且這個控制器的一些人沒有使用雄辯。 –

+0

是否有可能,如果我嘗試使用這種構建方法 public function checkingIfNotificationInserted(){ $ id = self :: getQueueableId(); return $ id; } –

+0

如果一些控制器不使用雄辯,然後切換它們,要麼到處使用雄辯,要麼根本不使用雄辯。如果你這樣做,那麼你可以使用事件處理程序。 laravel文檔涵蓋了在插入操作時觸發的事件,但如果使用原始數據庫插入操作,則不會觸發相同的事件。 – delatbabel