I have a form for the user edit his profile account. So it appears for each field the value if there is a value for each field with: "value="{{$user->name}}". But sometimes is appearing this error:
Trying to get property of non-object
Do you know how to correct the issue?
<form method="post" action="{{route('user.update')}}">
{{csrf_field()}}
<div>
<label for="name">Name</label>
<input type="text" value="{{$user->name}}" name="name" class="form-control" id="name">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" value="{{$user->surname}}" name="surname" class="form-control" id="surname">
</div>
<!-- other fields -->
<input type="submit" value="Update"/>
</form>
The update method:
public function updateGeneralInfo(Request $request){
$this->validate($request, [
'name' => 'required',
]);
$user = Auth::user();
$user->name = $request->name;
...
$user->save();
return redirect()->back();
}
In your controller you can do a check before you return the view:
if(Auth::check()){
//return view and other stuff
}
else {
//redirect to login
}
In your blade:
<form method="post" action="{{route('user.update')}}">
{{csrf_field()}}
<div>
<label for="name">Name</label>
<input type="text" value="{{auth()->user()->name}}" name="name" class="form-control" id="name">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" value="{{auth()->user()->surname}}" name="surname" class="form-control" id="surname">
</div>
<!-- other fields -->
<input type="submit" value="Update"/>
</form>