'Attempt to read property "id" on null - description.blade.php
I have a problem with id i get error
Attempt to read property "id" on null (View: /resources/views/admin/Attendance/description.blade.php)
view code that show error
<form method="post" action="{{route('time.outd',['id'=>$attend->id])}}" enctype="multipart/form-data" >
controller code:
public function outd(Request $request, $id)
{
$request->validate([
'out'=>'',
'description'=>'required',
]);
$attend=Attendance::where('employee_id',$id)->orderBy('id','desc')->first();
$attend->description=$request->get('description');
$attend->update([
'out' => Carbon::now()
]);
$attend->save();
return redirect()->route('time.index')->with('success', ' success.');
}
Solution 1:[1]
I think you're just a little off here. '$attend->id you are setting the id to a null value because you haven't set it. From the controller that is passing the view of this form you need to pass that id to the front. So your collection of $attend is available on the front. Should be something like this in your controller action.
return view('index', compact('attend'));
Then in your form action just post to a non wildcard route...
action="{{route('time.outd')}}"
Then in your form elements put...
<input type="hidden" name="id" value="{{$attend->id}}">
Then in your controller where this is being posted to your id will be...
$request->id
Now you know if it is null on the back-end post that there was no record from the model and that you need to create a new one. If it has an id then you can go on about your day processing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
