此片段来自我的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()
所以,我理解书模型删除评论不正常,但不明白如何解决。
答案 0 :(得分:1)
您可能需要使用正确的索引,外键更新数据库架构,并且正如@Razor所说,级联...请参阅http://laravel.com/docs/schema#foreign-keys