0

The error i am getting is "Call to undefined function App\belongsToMany".

This is one of the two models that is used for the relationship:

<?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();
}

}

My other model :

<?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');
}

}

The problem is, i can run the App\Category::find(1)->reviews; but, i can't run App\Review::find(1)->categories; It says "Call to undefined function App\BelongsToMany"

Nisthar
  • 89
  • 3
  • 13

1 Answers1

0

You have two errors in your categories() method.

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

The first error is the arrow. You put $this-belongsToMany but it should be $this->belongsToMany.

The second error is the namespace. It should be App\Category.

All together, the method should be:

public function categories()
{
    return $this->belongsToMany('App\Category','category_review','review_id','category_id')->withTimestamps();
}
Thomas Kim
  • 15,326
  • 2
  • 52
  • 42
  • Wow, Lost 1 day in that arrow:) Got it working now. Btw i got one question, what is the method index() on migrations do? – Nisthar Nov 29 '15 at 06:17
  • It adds a basic index. There's this stackoverflow thread that answers how indexes work: http://stackoverflow.com/questions/3567981/how-do-mysql-indexes-work – Thomas Kim Nov 29 '15 at 06:25