Skip to content

Instantly share code, notes, and snippets.

@shreesubhendu
Last active January 23, 2020 13:46
Show Gist options
  • Save shreesubhendu/77f9ae8f01e1563657cbd9bc83b847ee to your computer and use it in GitHub Desktop.
Save shreesubhendu/77f9ae8f01e1563657cbd9bc83b847ee to your computer and use it in GitHub Desktop.
Check whether record exists in, DB Laravel
// If you want to use the Model object if it exists:
$model = Model::where('column', '=', Input::get('column'))->first();
if ($model === null) {
// Model doesn't exist
}
// If only to check if exist or not
if (Model::where('column', '=', Input::get('column'))->count() > 0) {
// Model found
}
// If only to check if exist or not
if (Model::where('column', '=', Input::get('column'))->exists()) {
// Model found
}
try {
$model = Model::where('mobile', Input::get('column'))->firstOrFail();
// Model not found
} catch (ErrorException $e) {
// Model not found
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment