Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Laravel控制器中的不同标签中删除2行_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 从Laravel控制器中的不同标签中删除2行

Php 从Laravel控制器中的不同标签中删除2行,php,laravel,laravel-5,Php,Laravel,Laravel 5,因此,我有两个名为“topics”和“posts”的表 主题是针对帖子的主要内容和帖子的回复 所以,我想如果用户删除了它的主题,那么它下面的回复/帖子也应该被删除 这是我的删除表: {!! Form::open(['action' => ['TopicController@destroy', $topic->id], 'method' => 'POST', 'class' => 'pull-right']) !!} {{ Form::hidden('_method

因此,我有两个名为“topics”和“posts”的表

主题是针对帖子的主要内容和帖子的回复

所以,我想如果用户删除了它的主题,那么它下面的回复/帖子也应该被删除

这是我的删除表:

{!! Form::open(['action' => ['TopicController@destroy', $topic->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
    {{ Form::hidden('_method', 'DELETE') }}
    {{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{!! Form::close() !!}
这里是控制器:

$topic = Topic::find($id);
$post = Post::where('topic_id', $id)->get();
$topic->delete();
$post->delete();
return redirect('/')->with('Success', 'Post Removed');
但这是一个错误:

BadMethodCallException
Method delete does not exist.
这里哪里出错了???

使用级联删除

发件人:

还可以为约束的“删除时”和“更新时”属性指定所需的操作

posts
表迁移中定义外键约束:

$table->foreign('topic_id')
      ->references('id')
      ->on('topics')
      ->onDelete('cascade');
重新创建表,删除主题时,与主题相关的所有帖子将自动删除

更多信息,请访问


您应该在主题、帖子和回复模型中使用关系,例如,显示每个回复属于一篇帖子。

根据需要,尝试使用forceDelete()或softDelete()
Topic::destroy($id);
Post::where('topic_id', $id)->delete();
return redirect('/')->with('Success', 'Post Removed');