Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用未定义的函数App\belongstomy[Laravel]_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 调用未定义的函数App\belongstomy[Laravel]

Php 调用未定义的函数App\belongstomy[Laravel],php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我收到的错误是调用未定义的函数App\belongToMany 这是用于关系的两个模型之一: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Review extends Model { protected $table = "reviews"; protected $fillable = [ 'user_id','email','review']; public function user()

我收到的错误是调用未定义的函数App\belongToMany

这是用于关系的两个模型之一:

<?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::find1->reviews;但是,我无法运行App\Review::find1->categories;它表示调用未定义的函数App\belongtomany

在分类方法中有两个错误

第一个错误是箭头。您将$this belongtomany放入,但它应该是$this->belongtomany

第二个错误是名称空间。它应该是App\Category

总之,该方法应为:

public function categories()
{
    return $this->belongsToMany('App\Category','category_review','review_id','category_id')->withTimestamps();
}

哇,在那支箭中迷失了一天:现在它开始工作了。顺便说一句,我有一个问题,迁移的方法索引是什么?它添加了一个基本索引。有一个stackoverflow线程可以回答索引是如何工作的:
public function categories()
{
    return $this->belongsToMany('App\Category','category_review','review_id','category_id')->withTimestamps();
}