I am new to Laravel. Please excuse the newbie question but how do I find if a record exists?
$user = User::where(\'em
if (User::where('email', Input::get('email'))->exists()) {
// exists
}
if ($u = User::where('email', '=', $value)->first())
{
// do something with $u
return 'exists';
} else {
return 'nope';
}
would work with try/catch
->get() would still return an empty array
Created below method (for myself) to check if the given record id exists on Db table or not.
private 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);
Home It helps!
you can use laravel validation .
But this code is also good:
$user = User::where('email', $request->input('email'))->count();
if($user > 0)
{
echo "There is data";
}
else
echo "No data";
In laravel eloquent, has default exists() method, refer followed example.
if(User::where('id', $user_id )->exists()){
// your code...
}
In your Controller
$this->validate($request, [
'email' => 'required|unique:user|email',
]);
In your View - Display Already Exist Message
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif