Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaraBABA's avatar

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">&times;</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">&times;</span>
        </button>
        <strong>
            {!! session()->get('errors') !!}
        </strong>
    </div>
@endif

Any idea why the success works and not the error partial?

Thank you so much!

0 likes
6 replies
penkoad's avatar

Hi, Maybe you should go for a another var name, $errors is in Session. What is the Laravel version you're using ?

LaraBABA's avatar

Actually I have found one part of the problem and it is in my logic on this line:

    if($findUser->username != $request->get('username')){ 

If you look at it closer, it does not make sense because if the username is not replaced my condition will never jumped to else{

    return back()->with('errors', 'Sorry but this username has already been taken!');

Now I have rectified this issue and dd(0 the sessions I can see the error message being dumped but still get this error:

ErrorException (E_ERROR)
Call to a member function has() on string (View: C:\laragon\www\keepmaltaclean\resources\views\admin\users-edit.blade.php)

Here is my web.php route file, if there are any clues in this, I have the feeling the error is here somewhere and the page just cannot return.

Auth::routes();

//ADMIN ROUTES
Route::get('/admin', 'Admin\UserController@index')->name('viewusers')->middleware('role:admin');
Route::resource('users','Admin\UserController')->middleware('role:admin');//USERS CRUD
Route::post('users/{id}', 'Admin\UserController@update')->middleware('role:admin');
Snapey's avatar

$errors is used by the view for form validation issues and is expected to be a collection, here you return a string, so the error occurs because you cannot use has on a string

LaraBABA's avatar

Hi Snapey,

Thanks, I understand now.....the error now makes sense.... Great help! You seem to know this framework very very well, are you one of the coders who develop it?

Snapey's avatar

No, not me... I don't even code for a living! I've just been here a while.

Please or to participate in this conversation.