Attempt to read property "budget" on null

I'm having this issue where it throws this error on this piece of code
$project = Project::find(request()->route()->parameter('project'));
$budget = (int) $project->budget;
$project = Project::find(request()->route()->parameter('project'));
$budget = (int) $project->budget;
The property budget is not null and is read. if I
dd($budget)
dd($budget)
it shows the correct value if I
dd($project)
dd($project)
it shows obviously
App\Models\Project {#2355 ▼ // app/Filament/Condivisore/Resources/QuoteResource.php:56
#connection: "mysql"
#table: "projects"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:10 [▼
"id" => 7
"user_id" => 1
"title" => "Cerco un programmatore"
"slug" => "cerco-un-programmatore"
"banner" => "immagini-progetti/1/cerco-un-programmatore/banner/01HHHRMQWPP5T5E77MARYEWGNE.png"
"images" => "["immagini-progetti\/1\/cerco-un-programmatore\/immagini\/01HHHRMQYSV36TB4DF965ZFV1N.png","immagini-progetti\/1\/cerco-un-programmatore\/immagini\/01HHHRMR03WZH ▶"
"body" => "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ac diam turpis. Sed hendrerit mauris venenatis tortor volutpat, quis vulputate justo co ▶"
"created_at" => "2023-12-13 14:17:42"
"updated_at" => "2023-12-13 14:17:42"
"budget" => "25000"
]
#original: array:10 [▶]
#changes: []
#casts: array:1 [▼
"images" => "array"
]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:7 [▼
0 => "user_id"
1 => "title"
2 => "slug"
3 => "banner"
4 => "images"
5 => "body"
6 => "budget"
]
#guarded: array:1 [▼
0 => "*"
]
#queuedTags: []
App\Models\Project {#2355 ▼ // app/Filament/Condivisore/Resources/QuoteResource.php:56
#connection: "mysql"
#table: "projects"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:10 [▼
"id" => 7
"user_id" => 1
"title" => "Cerco un programmatore"
"slug" => "cerco-un-programmatore"
"banner" => "immagini-progetti/1/cerco-un-programmatore/banner/01HHHRMQWPP5T5E77MARYEWGNE.png"
"images" => "["immagini-progetti\/1\/cerco-un-programmatore\/immagini\/01HHHRMQYSV36TB4DF965ZFV1N.png","immagini-progetti\/1\/cerco-un-programmatore\/immagini\/01HHHRMR03WZH ▶"
"body" => "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ac diam turpis. Sed hendrerit mauris venenatis tortor volutpat, quis vulputate justo co ▶"
"created_at" => "2023-12-13 14:17:42"
"updated_at" => "2023-12-13 14:17:42"
"budget" => "25000"
]
#original: array:10 [▶]
#changes: []
#casts: array:1 [▼
"images" => "array"
]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:7 [▼
0 => "user_id"
1 => "title"
2 => "slug"
3 => "banner"
4 => "images"
5 => "body"
6 => "budget"
]
#guarded: array:1 [▼
0 => "*"
]
#queuedTags: []
3 Replies
Tim van Heugten
Tim van Heugten6mo ago
Guessing the code gets triggered again without the request parameter being present. Don’t dd, but log and see what happens.
Robbyn R
Robbyn R6mo ago
if you're on loop, please check all your data (maybe your dd only check first data)
DrByte
DrByte6mo ago
Since it's getting null back from the query, it means it couldn't find the record by that id. So you could also try determining what it was searching for:
$project = Project::find(request()->route()->parameter('project'));
+ if (is_null($project)) dd(request()->route());
$budget = (int) $project->budget;
$project = Project::find(request()->route()->parameter('project'));
+ if (is_null($project)) dd(request()->route());
$budget = (int) $project->budget;