【问题标题】:Laravel 5.7: Defining Relationships with Models having Reserved Names [duplicate]Laravel 5.7:定义与具有保留名称的模型的关系 [重复]
【发布时间】:2019-03-03 15:09:48
【问题描述】:

我浏览了这篇文章... Laravel - name a model using a reserved word

但这并没有提供定义关系的解决方案。

我有一个型号名称Class.php 我必须将它与学生和班级分配联系起来

这是我在 Student.php 中定义的关系:

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

我已经尝试了所有方法,现在我不想更改我的模型名称。我该如何进行这项工作?我在修补程序上不断收到此错误:

PHP 错误:语法错误,意外的 'Class' (T_CLASS),期待 /home/vagrant/Homestead/app/app/Class.php 中的标识符(T_STRING) 第 7 行

【问题讨论】:

  • 相关帖子中没有解释什么? PHP 中不能命名函数和类的保留字,与 Laravel 无关。您在使用其他语言时也会遇到同样的问题,因此您需要为它们命名。
  • 好的 @Aaryan,也发布你的 Class 模型。

标签: php laravel eloquent relationship laravel-5.7


【解决方案1】:

你不能使用保留字!!!!

作为建议(我在项目中所做的)是使用模型“ClassRoom”或其他名称的名称。

还有关系“classRooms()”之类的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    相关资源
    最近更新 更多