[英]How to delete record 1 and record 2 in different table using relationship in laravel 5.1
Here we go in my situation I have videos and video_tags table and it has a relationship one to many because in a video has many tags. 在这种情况下,我有视频和video_tags表,它之间存在一对多的关系,因为视频中有很多标签。 I'm trying to achieve to delete record1 and record2 storing in different table using relationship in one query only then the records will be deleted from table1 and table2.
我正在尝试使用一个查询中的关系来删除存储在不同表中的record1和record2,只有这样,记录才会从table1和table2中删除。 I think it is possible in laravel please i need help.
我认为有可能在拉拉韦尔,我需要帮助。
Model: 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
protected $table = 'videos';
protected $guarded = ['id'];
public function tags()
{
return $this->hasMany('App\VideoTag');
}
}
Controller: 控制器:
public function postDeleteVideo(Request $request, $id){
$tag = Video::find($id)->tags->delete();// <-- problem here
return response()->json(array('success'=>true));
}
What you need is to set CASCADE relationships in your database. 您需要在数据库中设置CASCADE关系。
Then... 然后...
$video = \\Video::find(1); $ video = \\ Video :: find(1);
$video->delete(); $ video->删除();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.