Thursday 24 January 2019

Laravel Method to check if the given record id exists on DB table or not.

public function isModelRecordExist($model, $recordId)
{
    if (!$recordId) return false;

    $count = $model->where(['id' => $recordId])->count();

    return $count ? true : false;
}

// To Test
$recordId = 5;
$status = $this->isModelRecordExist( (new MyTestModel()), $recordId);

// Outcome: true | false

Hope it helps!

No comments:

Post a Comment

Please post any queries and comments here.