skip to Main Content

I made controller for "CRUD" method to control database. everything is ok i can create, update and view any record but when I try to delete (destroy) record it cannot delete that specific record.

    public function destroy(blogs $blogs)
    {
        $blogs->delete();
        return redirect()->route('post.index')
        ->with('success','success create blog');

    }
<table class="table table-striped
    table-hover 
    table-borderless
    table-primary
    align-middle">
        <thead class="table-light">
            <caption>Table Name</caption>
            <tr>
                <th>No</th>
                <th>Post name</th>
                <th>details</th>
                <th>Image Name</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody class="table-group-divider">
                @foreach ($data as $item)
                <tr class="table-primary" >
                    <td scope="row">{{$item->id}}</td>
                    <td>{{$item->name}}</td>
                    <td>{{$item->description}}</td>
                    <td ><img style="width:10%" src="/images/{{$item->img}}" alt="" srcset=""></td>
                    <td>
                        <form action="{{route('post.destroy',$item->id)}} " method="post">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class='btn btn-danger'>delete</button>
                        </form>
                        <a href="{{route('post.edit',$item->id)}}" class="btn btn-primary">Edit</a>
                        <a href="{{route('post.show',$item->id)}}" class="btn btn-info">Show</a>
                    </td>
                </tr>
                @endforeach
               
            </tbody>
            <tfoot>
                
            </tfoot>
    </table> 
        {!! $data->links() !!}
   </div>

I already check database user permission but it still the same.

3

Answers


  1. Just Change $blogs with $blog in your controller method

    public function destroy(Blogs $blog)
    {
        $blog->delete();
        return redirect()->route('post.index')
        ->with('success','Deleted');
    
    }
    

    Also check model name; destroy(Blogs $blog) -> first(Blogs) should be exactly your model name.

    Login or Signup to reply.
  2. You Have To Find The Record First From Blog Table And Then Delete The Record.

    public function destroy($id)
    {

    $blogs = blogs::find($id);
    $blogs->delete();

    return redirect()->route(‘post.index’)
    ->with(‘success’,’success create blog’);

    }

    Login or Signup to reply.
    1. You have to check your route and your flow, I find it wrong like you want to delete blog but call post.destroy method, you have to check on which resource you are working on (Blog or Post). Best practice => make the things simple and do work on your naming convensions.

    2. You have to use your Model which should be ‘Blog’ not ‘Blogs’, check your model name and import your model accordingly.

    3. https://laravel.com/docs/10.x/controllers#resource-controllers, please read this once.

    4. First make it sure that your Model binding is working properly, check it by using dd($blog), if it show your data then it Good to go.

    5. If Model binding is not working properly, and you are not using Route::resource(‘blogs’, Your controller); then use id in your route like

    Route::delete('/blogs/{id}',  [BlogController::class, 'destroy'])->name('blogs.destroy');
    

    Then in controller’s destroy method:

    public function destroy(int $id)
        {
            $blog = Blog::query()->findOrFail($id);
            $blog->delete();
            return redirect()->route('blogs.index')
            ->with('success','success create blog');
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search