[英]Laravel book on delete remove related all posts and comments
这些摘录来自我的Book
模型:
class Book extends \Eloquent
{
public function delete()
{
// delete all related cover photos
$this->cover()->delete();
// delete all related comments photos
$this->posts()->comments()->delete();
// delete all related Blurbs
$this->posts()->delete();
// delete the model
return parent::delete();
}
}
在Post
模型中,我写道:
function book() {
return $this->belongsTo('Book');
}
function comments() {
return $this->hasMany('Comment');
}
function user() {
return $this->belongsTo('User');
}
public function delete()
{
// delete all related comments also
$this->comments()->delete();
// delete the model
return parent::delete();
}
在Comment
模型中,我写道:
function posts() {
return $this->belongsTo('Post');
}
function user() {
return $this->belongsTo('User');
}
现在,当我尝试删除这本书时,上面写着。 Call to undefined method Illuminate\\Database\\Query\\Builder::comments()
因此,我知道删除注释的书本模型无法正常工作,但不知道如何解决。
您可能需要使用正确的索引,外键并按@Razor所说的级联更新数据库架构,请参阅http://laravel.com/docs/schema#foreign-keys
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.