Skip to content
Advertisement

Laravel 8: delete() does not delete from the DB

I’m working with Laravel 8 to develop my forum and I wanted to add some Like and Unlike functionality to question that have been asked by users.

So Like function works fine but for Unlike, I got some problem.

Here is the method for Unliking:

public function destroy(Question $id, Request $request)
    {
        $request->user()->likes()->where('question_id', $id)->delete();

        return back();
    }

And here is the form behind this:

<form action="{{ route('destroy.likes', $show->id) }}" method="POST">
   @csrf
   @method('DELETE')
   <button class="btn">
      <i class="fas fa-thumbs-down"></i>
   </button>
</form>

So the problem is it does not delete the data from table likes at the DB.

I tried dd($request->user()); and dd($id);, they both exists with correct data, but I don’t know why delete() does not work properly.

So if you know how to solve this, please let me know, I would really appreciate any idea or suggestion from you guys…

Thanks in advance.

Advertisement

Answer

It looks like you are passing a Question object in your destroy method, and then passing the whole object in your where call.

Try changing your where call to where('question_id', $id->id), keeping in mind that your variable name is also called id. It may pay to clarify your variable name to $question.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement