DEV Community

Cover image for Delete Record in Laravel 8
Code And Deploy
Code And Deploy

Posted on

7 2

Delete Record in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/delete-record-in-laravel-8

In this short post, I will share simple methods for deleting records in Laravel 8 with examples.

Deleting Examples:

Single delete with Laravel query builder:

DB::table('posts')->where('id', 1)->delete();
Enter fullscreen mode Exit fullscreen mode

Multiple delete with Laravel query builder:

DB::table('posts')->whereIn('id', [2, 4])->delete();
Enter fullscreen mode Exit fullscreen mode

Single delete with Laravel eloquent:

Post::where('id', 1)->delete();
Enter fullscreen mode Exit fullscreen mode

Multiple delete with Laravel eloquent:

Post::whereIn('id', [2, 4])->delete();
Enter fullscreen mode Exit fullscreen mode

That's it. Next, we will implement in our controller example:

Delete Implementation

Controller code:

/**
* Remove the specified resource from storage.
*
* @param  \App\Models\Post  $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
    $post->delete();

    return redirect()->route('posts.index')
         ->withSuccess(__('Post delete successfully.'));
}
Enter fullscreen mode Exit fullscreen mode

Route code:

Route::resource('posts', PostsController::class);
Enter fullscreen mode Exit fullscreen mode

Blade code:

<!DOCTYPE html>
    <html>

    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Delete Record in Laravel 8 - codeanddeploy.com</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    </head>

    <body>
        <div class="container mt-5">
            @if(Session::get('success', false))
              <?php $data = Session::get('success'); ?>
              @if (is_array($data))
                  @foreach ($data as $msg)
                      <div class="alert alert-success" role="alert">
                          <i class="fa fa-check"></i>
                          {{ $msg }}
                      </div>
                  @endforeach
              @else
                  <div class="alert alert-success" role="alert">
                      <i class="fa fa-check"></i>
                      {{ $data }}
                  </div>
              @endif
            @endif
            <table class="table table-striped" id="users-table">
              <thead>
                <tr>
                  <th scope="col">Title</th>
                  <th scope="col">Description</th>
                  <th scope="col">Body</th>
                  <th scope="col">Delete</th>
                </tr>
              </thead>
              <tbody>
                @foreach($posts as $post)
                  <tr>
                    <td>{{$post->title}}</td>
                    <td>{{$post->description}}</td>
                    <td>{{$post->body}}</td>
                    <td>
                        <form method="post" action="{{route('posts.destroy',$post->id)}}">
                            @method('delete')
                            @csrf
                            <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                        </form>
                    </td>
                  </tr>
                @endforeach
              </tbody>
            </table>
        </div>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/delete-record-in-laravel-8 if you want to download this code.

Happy coding :)

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay