Home » Laravel FindOrFail: An Extensive Tutorial

Laravel FindOrFail: An Extensive Tutorial

Loved it? Please Share on Social Media

Introduction:

If you want to search your model instances via a primary key/ID, Then you are in the right place; we will discuss the laravel findorfail function in this exclusive tutorial. Hey, What’s up, everybody? I hope you are doing fine. Today, we will discuss the laravel findorfail function. Laravel findorfail function is an excellent function for searching database records using primary key/ID. 

Another essential feature of this function is if your searched model instance is not present, then it will throw 404 errors automatically. If we don’t have this function, we have to integrate this feature manually, which is a totally time & effort-worthy task.

So, in this tutorial, we’ll learn how to use this function on our laravel application and experiment with the features of this function appropriately.

Getting our project ready for laravel findorfail function:

First, we will prepare our project by downloading Laravel from the internet and configuring our database. Lastly, we will add some users by registering on our application.

Creating a view file for the list user:

Now, create a users.blade.php file inside the views directory of our application. Then, copy and paste the code below into that file directly.

<!doctype html>
<html>
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css">

    <title>Laravel FindOrFail Tutorial</title>
  </head>
  <body>
    <table class="table">
  <thead>
    <tr>
      <th>ID</th>
      <th>NAME</th>
      <th>EMAIL</th>
      <th>#</th>
    </tr>
  </thead>
  <tbody>
    @foreach($users as $user)
      <tr>
        <th>{{ $user->id }}</th>
        <th>{{ $user->name }}</th>
        <th>{{ $user->email }}</th>
        <th>
          <a href="{{ url('/edit/user/'.$user->id) }}" class="btn btn-primary">EDIT</a>
        </th>
      </tr>
    @endforeach
  </tbody>
</table>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>

Creating a logic for list user and edit user:

public function user(){
    $users = App\Models\User::all();
    return view('users', [
        'users' => $users
    ]);
}
public function user($id) {
    $user = App\Models\User::findOrFail($id);
    return view('edit-user', [
        'user' => $user
    ]);
}

Making route logic for laravel findorfail function:

Route::get('users', [App\Http\Controllers\UserController::class, 'users']);
Route::get('user/{id}', [App\Http\Controllers\UserController::class, 'user']);

Relevant Note for Laravel FindOrFail: 

You can pass multiple IDs inside laravel findorfail as an array. You can check out the example code snippet below to learn more.

$users = App\Models\User::findOrFail([4, 6, 10]);
dd($users);

As you can see from the above code, this function can get an array of results for search database columns.

Test The Application:

At last, we will start our local server using our artisan command. php artisan serve If there is no problem, our code will work fine, and the project will be running now at http://127.0.0.1:8000/ or http://locahost:8000/. You can know more from their documentation.

Conclusion:

Finished! That’s all about our tutorial. Although this tutorial cannot make you an expert in the Laravel findorfail function, it will give you some fundamental knowledge of how to work with this kind of thing in the future. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.