使用未定义的常量id-假定';id';(这将在PHP的未来版本中引发错误)

使用未定义的常量id-假定';id';(这将在PHP的未来版本中引发错误),php,laravel,eloquent,resources,Php,Laravel,Eloquent,Resources,我有一个jsonresource,但它说的是未定义的常量id } 我的控制器中的代码 但是当我返回DashboardGraphData::all()而不是将其放入DashboardGraphDataResource::collection()时,结果显示出来 我的查询是否有问题?请帮助我:( 使用$this->id而不仅仅是id return [ 'id' => $this->id, 'technology' => $this->techn

我有一个jsonresource,但它说的是未定义的常量id

}

我的控制器中的代码

但是当我返回DashboardGraphData::all()而不是将其放入DashboardGraphDataResource::collection()时,结果显示出来

我的查询是否有问题?请帮助我:(


使用
$this->id
而不仅仅是
id

return [
        'id' => $this->id,
        'technology' => $this->technology,
        'area' => $this->area,
        'totalCapacity' => $this->total_capacity,
        'working' => $this->working,
        'fileDate' => $this->file_date,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];

使用
$this->id
而不仅仅是
id

return [
        'id' => $this->id,
        'technology' => $this->technology,
        'area' => $this->area,
        'totalCapacity' => $this->total_capacity,
        'working' => $this->working,
        'fileDate' => $this->file_date,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];

使用$request->id insted id

return [
    'id' => $request->id,
    'technology' => $request->technology,
    'area' => $request->area,
    'totalCapacity' => $request->total_capacity,
    'working' => $request->working,
    'fileDate' => $request->file_date,
    'created_at' => $request->created_at,
    'updated_at' => $request->updated_at,
];

使用$request->id insted id

return [
    'id' => $request->id,
    'technology' => $request->technology,
    'area' => $request->area,
    'totalCapacity' => $request->total_capacity,
    'working' => $request->working,
    'fileDate' => $request->file_date,
    'created_at' => $request->created_at,
    'updated_at' => $request->updated_at,
];

这更多的是一个PHP错误,而不是Laravel错误

常数与局部变量: 您的函数接受一个名为
$request
的参数。request参数包含您传递给它的所有信息。我假设您传递的数组或对象包含键/属性
id
technology
区域
总容量
工作
文件日期
处创建,
处更新

代码的问题是,您正在试图填充的每个数组值中调用一个常量。PHP中的常量是具有固定值的名称或标识符。它们与变量类似,只是一旦定义,就无法修改/更改

在PHP中,常量以字母或下划线开头,在常量名称之前没有
$
符号

在您的情况下,我认为您正在尝试从
$request
对象/数组访问属性/键值


您可以通过执行
$request->property\u name\u here
$request['key\u name\u here']
来访问它们,并用值填充数组。希望这有所帮助。

这更多的是PHP错误,而不是Laravel错误。我将解释一些事情

常数与局部变量: 您的函数接受一个名为
$request
的参数。request参数包含您传递给它的所有信息。我假设您传递的数组或对象包含键/属性
id
technology
区域
总容量
工作
文件日期
处创建,
处更新

代码的问题是,您正在试图填充的每个数组值中调用一个常量。PHP中的常量是具有固定值的名称或标识符。它们与变量类似,只是一旦定义,就无法修改/更改

在PHP中,常量以字母或下划线开头,在常量名称之前没有
$
符号

在您的情况下,我认为您正在尝试从
$request
对象/数组访问属性/键值


您可以通过执行
$request->property\u name\u here
$request['key\u name\u here']
并用值填充数组。希望这有帮助。

$request->id,返回null与其余的相同。答案实际上并没有指出问题代码的错误以及为什么您的代码是正确的。您能在回答中解释一下吗?$request->id,返回null与其余的相同。答案实际上并不正确指出问题代码的错误,以及为什么您的代码是正确的。请您在回答中解释一下?
return [
    'id' => $request->id,
    'technology' => $request->technology,
    'area' => $request->area,
    'totalCapacity' => $request->total_capacity,
    'working' => $request->working,
    'fileDate' => $request->file_date,
    'created_at' => $request->created_at,
    'updated_at' => $request->updated_at,
];