使用保留字命名模型

使用保留字命名模型

本文介绍了Laravel-使用保留字命名模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是无法使用PHP中的保留字作为类名,但是我的Laravel应用启用了课程和类的管理,因此我有一个名为classes的数据库表,一个ClassController以及一个希望的Class模型.但是,在Laravel中,我的模型必须命名为Class,这在PHP中是不允许的:

I know it's not possible to use a reserved word in PHP as class name, but my Laravel app enables managing of courses and classes, so I have a database table called classes, a ClassController and hopefully a Class model. However, in Laravel, my model has to be named Class which is not allowed in PHP:

class Class extends Model {}

使用同义词,最接近的是Lecture,似乎还是不正确.

Using a synonym, the closest being Lecture, still doesn't seem right.

是否有Laravel解决方法?

Is there a Laravel workaround to this?

推荐答案

Laravel不会强迫您为模型或控制器使用特定名称.这只是一个约定.您可以根据需要命名模型.您要做的唯一修改就是在模型中声明protected $table = 'classes'

Laravel doesn't force you to use specific names for your models or controllers. It's just a convention. You can name your model whatever you want it to be. the only amend you will have to make is to declare protected $table = 'classes' in your model

这篇关于Laravel-使用保留字命名模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:26