我该如何检查表中的一行是否插入了laravel雄辩


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

在查询DB中的所有信息之前,我想检查表中的一行是否被插入,如果没有任何新的插入,我将不使用查询方法。但我非常了解如何通过Eloquent库检查它是否插入Laravel5中。

我研究了一些教程,发现如下sql句子,但我不知道如何在Eloquent中使用它,这个sql是否如我所期望的那样工作。

在检查IfNotificationUpdated方法时,我想只在将一行插入表中时检查true和false,但由于这句话,它将查询所有数据

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

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

在控制器中:

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

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

假设你做了这样的插入:

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

或者这个:

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

然后您可以进行以下检查:

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