[英]Delete Data From all tables using Load() Laravel 5.5
i am trying to delete Model from Model table and all of its comments from Comments table...my current code delete the model but not its comments from the other table... 我正在尝试从模型表中删除模型,并从注释表中删除其所有注释...我当前的代码删除模型,但不删除其他表中的注释...
Here is my destroy function 这是我的破坏功能
public function destroy(Request $request,CadModel $cadmodel)
{
$cadmodel->load('comments')->delete();
$request->session()->flash('message.level', 'success');
$request->session()->flash('message.content', 'File Deleted Successfully!');
return redirect()->route('dashboard');
}
Here is the relationship i defined in CadModel Laravel Model... 这是我在CadModel Laravel模型中定义的关系...
public function comments()
{
return $this->hasMany(Comment::class);
}
Please Reply and suggest me a solution to delete both..Model and its Comments 请回复并建议我删除..Model及其注释的解决方案
In your migration, set your related comments to be deleted if the model is deleted with onDelete('cascade')
. 在迁移中,如果使用
onDelete('cascade')
删除了模型,则设置要删除的相关注释。
Example: 例:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments_model', function(Blueprint $table)
{
$table->increments('id');
$table->integer('comment_id')->unsigned();
$table->integer('model_id')->unsigned();
$table->foreign('comment_id')->references('id')->on('comments')->onDelete('cascade');
$table->foreign('model_id')->references('id')->on('models')->onDelete('cascade');
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.