简体   繁体   English

Laravel 8:delete() 不会从数据库中删除

[英]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.我正在使用 Laravel 8 来开发我的论坛,我想为用户提出的问题添加一些喜欢和不喜欢的功能。

So Like function works fine but for Unlike, I got some problem.所以就像 function 工作正常,但对于不同,我遇到了一些问题。

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.所以问题是它不会从数据库中的表中likes数据。

I tried dd($request->user());我试过dd($request->user()); and dd($id);dd($id); , they both exists with correct data, but I don't know why delete() does not work properly. ,它们都存在正确的数据,但我不知道为什么delete()不能正常工作。

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.提前致谢。

It looks like you are passing a Question object in your destroy method, and then passing the whole object in your where call.看起来你在你的destroy方法中传递了一个问题object ,然后在你的where调用中传递了整个 object。

Try changing your where call to where('question_id', $id->id) , keeping in mind that your variable name is also called id .尝试将您的where调用更改为where('question_id', $id->id) ,请记住您的变量名也称为id It may pay to clarify your variable name to $question .将您的变量名称澄清为$question可能会有所帮助。

There is a problem with your query, when you get the records that you want to delete:当您获取要删除的记录时,您的查询有问题:

app/Http/routes.php app/Http/routes.php

Route::get('delete-records','YourController@index');
Route::get('delete/{id}','YourController@destroy')->name('destroy.likes');

Your View:您的看法:

<a href="{{ route('destroy.likes', ['id' => $show->id]) }}" title="@lang('')">
   Delete
</a>

Controller Destroy Method: Controller 销毁方法:

public function destroy($id){
  $question = Question::find($id);
  $question ->delete();
  return back;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM