Latest laravel and default settings
Many reports of "Access to an undefined property IlluminateDatabaseEloquentModel::$attribute."
Hi,
Can you share any code sample where this error happens? Also please follow the issue template for new issues.
That would make it possible for us to help you.
I'm seeing it about all of my resources and their fields (which are standard) i.e.
Access to an undefined property App\Http\Resources\UserResource::$first_name.
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'first_name' => $this->first_name,
...
Some other places like service providers:
Providers/RouteServiceProvider.php
101 Access to an undefined property Illuminate\Foundation\Application::$request.
157 Access to an undefined property Illuminate\Foundation\Application::$request.
164 Access to an undefined property Illuminate\Foundation\Application::$request.
Route::bind('risks', function($value, \Illuminate\Routing\Route $route) {
$dpia = app()->request->route('dpias');
A lot of my traits:
Traits/MorphsToManySecurityMeasures.php (in context of class App\Models\Org\System)
Access to an undefined property Illuminate\Database\Eloquent\Model::$id.
public static function bootMorphsToManySecurityMeasures()
{
static::deleted(function(Model $model) {
\DB::table('security_measurables')
->where('security_measurable_type', $model->getMorphClass())
->where('security_measurable_id', $model->id)
->delete();
});
}
Thank you for your _complete_ report!
To me these seem to be three slightly different issues.
@szepeviktor thanks for your _helpful_ comments!
The first problem can be solved by adding the following PHPDoc annotation to your resource class:
/** @extends JsonResource<User> */
as described here.
The second problem can be resolved by changing it to either
$dpia = request()->route('dpias');
or
$dpia = app('request')->route('dpias');
The last problem is expected behavior, because Model does not have an id by definition. You can either ignore it by adding e.g. // @phpstan-ignore-next-line, or you could create a custom Model class:
/**
* @property int $id
*/
class MyCustomModel extends Model {}
and use that as a typehint.
Most helpful comment
The first problem can be solved by adding the following PHPDoc annotation to your resource class:
as described here.
The second problem can be resolved by changing it to either
or
The last problem is expected behavior, because
Modeldoes not have anidby definition. You can either ignore it by adding e.g.// @phpstan-ignore-next-line, or you could create a custom Model class:and use that as a typehint.