提问人:Latte cofie 提问时间:10/11/2023 最后编辑:ADysonLatte cofie 更新时间:10/12/2023 访问量:42
laravel 中的废话错误 [语法错误:意外的令牌“$table”PHP] 有什么问题?[关闭]
Nonsense error in laravel [Syntax error: unexpected token '$table'PHP] what is the problem? [closed]
问:
我将我的表名给了我创建的模型文件,它给出了语法错误
语法错误:意外的标记“$table”
答:
2赞
Nicolas
10/11/2023
#1
变量是一个类属性,类属性需要用前缀 ( 或 ) 来定义其可见性。$table
public
protected
private
在这种特殊情况下,属性已经在 上定义了,所以我们知道它是可见性的,它是 。$table
Model
protected
class YourModel extends Model {
protected $table = "your table";
}
评论
0赞
Latte cofie
10/11/2023
谢谢问题解决了,但为什么我们需要这样做?
0赞
Nicolas
10/11/2023
需要定义如何访问类属性。以前,将可见性排除在外将默认为“public”,但是,省略它是一种不好的做法。现在,php 甚至不允许您省略可见性。
0赞
Latte cofie
10/11/2023
我明白了,谢谢你,先生。
0赞
Tanmay Modi
10/12/2023
#2
我们必须为该类属性定义访问修饰符。
餐桌使用:-protected $table = "users"
您可以为表定义主键字段名称,以便模型知道这一点。
protected $primaryKey = "code";
如果模型具有非整数数据类型主键字段,则必须在模型中定义以下这些字段 示例:在您的模型中,代码是主键,因此根据它是字符串数据类型,因此您必须按照以下示例进行定义。
protected $keyType = 'string';
评论
Parse error: syntax error, unexpected variable "$table", expecting "function" or "const"