调用未定义函数 App\belongsToMany [Laravel]
Call to undefined function App\belongsToMany [Laravel]
我收到的错误是 "Call to undefined function 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;它说 "Call to undefined function App\BelongsToMany"
您的 categories()
方法有两个错误。
public function categories()
{
return $this-belongsToMany('Category','category_review','review_id','category_id')->withTimestamps();
}
第一个错误是箭头。你输入 $this-belongsToMany
但它应该是 $this->belongsToMany
.
第二个错误是命名空间。应该是 App\Category
.
加在一起,方法应该是:
public function categories()
{
return $this->belongsToMany('App\Category','category_review','review_id','category_id')->withTimestamps();
}
我收到的错误是 "Call to undefined function 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;它说 "Call to undefined function App\BelongsToMany"
您的 categories()
方法有两个错误。
public function categories()
{
return $this-belongsToMany('Category','category_review','review_id','category_id')->withTimestamps();
}
第一个错误是箭头。你输入 $this-belongsToMany
但它应该是 $this->belongsToMany
.
第二个错误是命名空间。应该是 App\Category
.
加在一起,方法应该是:
public function categories()
{
return $this->belongsToMany('App\Category','category_review','review_id','category_id')->withTimestamps();
}