-1

I went through this post... Laravel - name a model using a reserved word

But this doesn't provide a solution of defining relationships.

I have a Model name Class.php And I have to Relate it with Student & ClassAllocation

Here is my Relationship defined in Student.php:

/**
 * Check the Class allocated to Student
 *
 *@return void
 */
public function class()
{
    return $this->belongsToMany('App\Class', 'class_allocations');
}

I have tried everything and I don't want to change my Model name now. How do I make this work? I keep getting this error on tinker:

PHP Error: syntax error, unexpected 'Class' (T_CLASS), expecting identifier (T_STRING) in /home/vagrant/Homestead/app/app/Class.php on line 7

James Jones
  • 3,850
  • 5
  • 25
  • 44
Aaryan Kapoor
  • 33
  • 1
  • 3
  • 1
    What wasn't explained in the related post? You can't name functions and classes reserved words in PHP, has nothing to do with Laravel. You'd have the same issues with other languages so you need to name them something else. – Devon Bessemer Sep 27 '18 at 16:39
  • Ok @Aaryan, post your Class model as well. – Elisha Senoo Sep 27 '18 at 16:40

1 Answers1

0

you can't use reserved words!!!!

As a suggestion (what I did in my project) is use the name for the model 'ClassRoom' or something.

And the name for the relationship 'classRooms()' or something.

Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28