【发布时间】:2015-11-29 06:05:01
【问题描述】:
我得到的错误是“调用未定义的函数 App\belongsToMany”。
这是用于关系的两个模型之一:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
protected $table = "reviews";
protected $fillable = [ 'user_id','email','review'];
public function user()
{
return $this->belongsTo('App\User');
}
public function votes()
{
return $this->belongsToMany('App\User')->withPivot('vote')->withTimestamps();
}
public function categories()
{
return $this-belongsToMany('Category','category_review','review_id','category_id')->withTimestamps();
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
}
我的另一个模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function reviews()
{
return $this->belongsToMany('App\Review','category_review','category_id','review_id')->withTimestamps();
}
public function children()
{
return $this->hasMany('App\Category','parent_id');
}
public function parent()
{
return $this->belongsTo('App\Category','parent_id');
}
}
问题是,我可以运行 App\Category::find(1)->reviews;但是,我无法运行 App\Review::find(1)->categories;它说“调用未定义的函数 App\BelongsToMany”
【问题讨论】:
标签: php laravel laravel-5 eloquent