[英]Call to undefined function App\belongsToMany [Laravel]
我得到的錯誤是“調用未定義函數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”
您的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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.