vendredi 9 avril 2021

Can't delete first post added by a user in laravel

I am working on social media website with laravel in which posts added by user is listed in user's profile and user can delete the post ,but everything is working perfectly except user can't delete the first post added. Why it is happening. I think the delete function from PostController is not calling when trying to delete first post. I am giving my code

Route::delete('/deletepost/{id}', 'PostController@delete')->name('deletepost');

PostController.php

public function delete($id)
{
    Post::find($id)->delete();
    if (Auth::user()->id == 1) {
        return redirect(route('admin.dashboard'))
            ->with('successMsg', 'Place Deleted Successfully');
    }else{
        return redirect(route('author.dashboard'))
            ->with('successMsg', 'Place Deleted Successfully');
    }
}

code in dashboard.blade.php

@if(Auth::user()->posts)
    @foreach(Auth::user()->posts as $post)
    <div class="column">
    <div class="col-sm-4">

        <form id="delete-form-"  action="" method="post" style="display:none">
        
        
        </form>

        <div class="panel panel-primary">
        <a href=""><button type="button" class="btn btn-link">Edit</button></a>
        <button type="button"
        onclick="if(confirm('Are you sure to delete this post?')){
            event.preventDefault();
            document.getElementById('delete-form-').submit();
        }else{
            event.preventDefault();
        }"
        class="btn btn-link">Delete</button>
        <div class="panel-heading"></div>
        <a href=""><div class="panel-body"><img src="" class="img-responsive" style="width:100%;height:140px" alt="Image"></div></a>
        <div class="panel-footer"></div>
        </div>
    </div>
    </div>

    @endforeach
@endif

Note: delete function is working perfectly when deleting posts except first post and i am not getting any errors messages



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire