试图仅在我的网站上获取非对象的 属性 'id'

Trying to get property 'id' of non-object only in my website

最近我更新了我的网站,我只在一个视图中发现了这个错误,在我的本地主机上工作正常,我通过 FTP 上传了它。我要疯了!

[2020-01-30 03:32:39] local.ERROR: Trying to get property 'id' of non-object (View: /home/csinf298/resources/views/addEdit_promotion.blade.php) {"userId":3,"email":"email@email.com.mx","exception":"[object] (ErrorException(code: 0): Trying to get property 'id' of non-object (View: /home/csinf298/resources/views/addEdit_promotion.blade.php) at /home/csinf298/storage/framework/views/a22f101b5a2d4fafaa9530cd13c1077aebc547ce.php:211, ErrorException(code: 0): Trying to get property 'id' of non-object at /home/csinf298/storage/framework/views/a22f101b5a2d4fafaa9530cd13c1077aebc547ce.php:211)

My.blade

$promotion->id

我也试过了

$promotion->['id']

[2020-01-30 03:58:27] local.ERROR: Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' (View: /home/csinf298/resources/views/addEdit_promotion.blade.php) {"userId":3,"email":"carlos.cisneros@csinformatica.com.mx","exception":"[object] (ErrorException(code: 0): Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' (View: /home/csinf298/resources/views/addEdit_promotion.blade.php) at /home/csinf298/storage/framework/views/a22f101b5a2d4fafaa9530cd13c1077aebc547ce.php:176, Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' at /home/csinf298/storage/framework/views/a22f101b5a2d4fafaa9530cd13c1077aebc547ce.php:176)

首先,使用以下命令$promotion->id清除缓存

php artisan config:cache
php artisan view:clear
php artisan route:clear

如果您仍然收到错误,请使用 $promotion->userId,正如我在您的错误中看到的那样,对象包含 userId,而不是 id.

您的查询返回的是数组还是对象?如果将其转储出来,您可能会发现它是一个数组,而您所需要的只是数组访问 ([]) 而不是对象访问 (->)。 要么 试试这个

$promotion->id

$promotion->['id']

$promotion[0]->poster['id']
$promotion->poster[0]['id']

碰巧一段时间后我们需要运行

 php artisan passport:install --force 

再次生成密钥,这解决了我的问题,

Trying to get property 'id' of non-object

如您所见,它说 non-object 所以您需要首先检查它是数组还是对象,如果它是对象,然后检查 id 属性 是否存在 如果它是数组那么 而不是 $promotion->['id']

使用这个

$promotion['id']