Hi, Maybe you should go for a another var name, $errors is in Session. What is the Laravel version you're using ?
Oct 28, 2017
6
Level 10
Stuck with ->back() and session message not showing up
Hello,
I have been for a couple of hours on this one and cannot find an answer to it.
public function update(Request $request, $id)
{
//Find user details
$findUser = DB::table('users')->where('id', '=', $id)->first();
$this->validate(request(), [
'name' => 'required|string|min:2|max:64',
'surname' => 'required|string|min:2|max:64',
'username' => 'required|string|min:2|max:64',
'email' => 'required|string|email|max:128',
'town_id' => 'required|integer',
'password' => 'required|string|min:6'
]);
//If username from the form is not the same as username from database
if($findUser->username != $request->get('username')){
//Check if username exists in the whole database
$usernameExists = DB::table('users')->where('username', '=', $request->get('username'))->first();
//If username does not exists
if(!$usernameExists){
//Add this new username to user profile
$updateUsername = DB::table('users')->where('id', '=', $id)
->update(array('username' => $request->get('username')));
return back()->with('success', 'Username updated!');
}else{
return back()->with('errors', 'Sorry but this username has already been taken!');
}
}
}
I am on the edit page of a user's profile
When the username does not exists, the message:
return back()->with('success', 'Username updated!');
shows up correctly on return.
But when the user exists, this session:
return back()->with('errors', 'Sorry but this username has already been taken!');
Causes the error:
ErrorException (E_ERROR)
Call to a member function has() on string (View: C:\laragon\www\myproject\resources\views\admin\users-edit.blade.php)
The weird thing is that both the error and success pages are identical:
Partial success
@if (session()->has('success'))
<div class="alert alert-success text-center animated fadeIn">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>
{!! session()->get('success') !!}
</strong>
</div>
@endif
Partial error:
@if (session()->has('errors'))
<div class="alert alert-danger animated fadeIn text-center">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>
{!! session()->get('errors') !!}
</strong>
</div>
@endif
Any idea why the success works and not the error partial?
Thank you so much!
Please or to participate in this conversation.