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

ABasso's avatar
Level 34

empty data.

i'm trying to pass some data to blade template. but seeing not working for me.

passing data.

return redirect()->back()->with('RegisterUserModal', '#RegisterUserModal');

getting the data.

$({{ $errors->has('RegisterUserModal') ? $errors->get('RegisterUserModal') : '' }}).modal('show')

if I do a var_dump it is empty.

anyone know why this happen?

sorry if I do not write with good English.

0 likes
27 replies
ohffs's avatar

I think it should be :

redirect()->back()->withErrors(['RegisterUserModal' => '#RegisterUserModal']);
1 like
ABasso's avatar
Level 34

did I try

redirect()->back()->withErrors(['RegisterUserModal' => '#RegisterUserModal']);

but nothing not pass to blade template.

if I do a var_dump isn't there.

{{ var_dump(Session::all) }}
```
ABasso's avatar
Level 34

I find a fix

@if($errors->all())
    $('#RegisterUserModal').modal('show');
@endif
nickywest's avatar

I do believe the problem you had was with the redirect.

You chained redirect()->back()->with(). Just do back()->with() instead and the with should carry in. You don't need that first redirect().

ABasso's avatar
Level 34

thanks for the help . but still not working.

return back()->with('RegisterUserModal', '#RegisterUserModal');

var_dump show me bool(false)

{!! var_dump($errors->has('RegisterUserModal')) !!}
anthonyjean's avatar

You are trying to get the data from the $errors variable. It would work if you used ->withErrors. But here you are using "with". When you pass data using the "with" method, it flashes the data into the session. So try something like Session::has('RegisterUserModal') and then retrieve it using Session::get('RegisterUserModal').

ABasso's avatar
Level 34

I did try

{!! var_dump(Session::has('RegisterUserModal')) !!}}

I get same bool(false)

ABasso's avatar
Level 34
return back()->with('RegisterUserModal', '#RegisterUserModal');

bool(false)

{!! var_dump(Session::has('RegisterUserModal')) !!}}

or

back()->withErrors(['RegisterUserModal' => '#RegisterUserModal']);

bool(false)

{!! var_dump($errors->has('RegisterUserModal')) !!}
SaeedPrez's avatar

Check php artisan route:list to make sure the web middleware is present on all your routes and that it's only applied once.

ABasso's avatar
Level 34

yes the route is middleware web.

ABasso's avatar
Level 34

middleware web on laravel 5.2 is by default

SaeedPrez's avatar

@kaxias

Try putting this in your routes file then go to yoursite/test, see if it works..

Route::get('/test', function() {
    return redirect('/test2')->with('test', 'Test is working!');
});

Route::get('/test2', function() {
    dd(session()->get('test') ?? 'Test is NOT working');
});

If it works, take a look at your controller, the one you redirect back to.. what does it return.. view or redirect? If you want, post it here so we can take a look.

ABasso's avatar
Level 34

php artisan route:list

+--------+----------+---------+---------+-----------------------------------------------------+------------+  
| Domain | Method   | URI     | Name    | Action                                              | Middleware |  
+--------+----------+---------+---------+-----------------------------------------------------+------------+  
|        | GET|HEAD | /       | home    | App\Http\Controllers\HomeController@home            | web        |  
|        | POST     | signin  | signin  | App\Http\Controllers\Auth\AuthController@signIn     | web        |  
|        | GET|HEAD | signout | signout | App\Http\Controllers\Auth\AuthController@signOut    | web        |  
|        | POST     | signup  | signup  | App\Http\Controllers\Auth\AuthController@postSignup | web        |  
+--------+----------+---------+---------+-----------------------------------------------------+------------+  

registerUsers trait

<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use Illuminate\Http\Request;

trait RegisterUsers
{
    public function postSignup(Request $request)
    {
        $validatoe = $this->validaterSignUp($request);
        if($validate->failed()) {
            return back()->with('RegisterUserModal', '#RegisterUserModal');
        }
        User::createUser($request->only('username', 'email', 'passwordR'));
        return back();
    }

    protected function validaterSignUp(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|min:3|max:50|unique:users,username',
            'email' => 'required|email|min:3|unique:users,email',
            'passwordR' => 'required|min:3|max:50',
            'confirm_password' => 'required|min:3|max:50|same:passwordR',
        ]);
    }
}
ABasso's avatar
Level 34

that did work ok.

Route::get('/test', function() {
    return redirect('/test2')->with('test', 'Test is working!');
});

Route::get('/test2', function() {
    dd(session()->get('test') ?? 'Test is NOT working');
});
ABasso's avatar
Level 34

input and validation errors work ok.

SaeedPrez's avatar

Can you see it?

        $validatoe = $this->validaterSignUp($request); // <-- misspelled variable
        if($validate->failed()) { // <-- never true, hence never sending data
            return back()->with('RegisterUserModal', '#RegisterUserModal');
        }
ABasso's avatar
Level 34
$validatoe

this was my mistake a few minutes ago when I was editing and not realize. no longer worked before that

SaeedPrez's avatar

@kaxias alright, well.. we've at least shown that Laravel is not bugged. I've only seen parts of your code so I can't tell what's happening exactly, but check through it and maybe even start simpler, test it, get it to work, then extract to traits and whatnot.

ABasso's avatar
Level 34

so you think can be because this is in trait?

SaeedPrez's avatar

No, not because it's in a trait. My point was you've started on a complicated level, and now that you've run into a problem, it's more difficult to find what's causing it and it's also difficult for us to help you because we can't see all of your code and we don't know what's going on.

Edit

Let me give you an example. Let's say you start a new project and just add a simple login, no validation or anything, just simple 3 line authentication. Auth attempt, return true or false. You test that and get it to work, then maybe you add return a view, you test that, then you add validation, then you test that.. then you extract some of the logic to it's on method, then you test that, then you move that working method to a trait and test that, and so on..

Since you're taking such small steps and testing every step, any errors would mean you only have to take the smallest step back, fix it, move forward.

ABasso's avatar
Level 34

router.php

<?php

Route::group(['prefix' => '/'], function ($route) {
    $route->get('', ['as' => 'home', 'uses' => 'HomeController@home']);

    $route->post('signin', ['as' => 'signin', 'uses' => 'Auth\AuthController@postSignin']);
    $route->post('signup', ['as' => 'signup', 'uses' => 'Auth\AuthController@postSignup']);
    $route->get('signout', ['as' => 'signout', 'uses' => 'Auth\AuthController@signOut']);
});

AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    use LoginUsers, RegisterUsers;

    public function signOut()
    {
        auth()->guard()->logout();
        return redirect(route('home'));
    }
}

LoginUsers.php

<?php

namespace App\Http\Controllers\Auth;

use Lang;
use Illuminate\Http\Request;

trait LoginUsers
{
    public function postSignin(Request $request)
    {
        $this->validaterSignIn($request);
        $credentials = $this->getCredentials($request);
        if (auth()->guard()->attempt($credentials, $request->has('remember'))) {
            return redirect()->intended();
        }
        return $this->sendFailedSignInResponse($request);
    }

    protected function validaterSignIn(Request $request)
    {
        return $this->validate($request, [
            'identifier' => 'required',
            'passwordL' => 'required',
        ]);
    }

    protected function sendFailedSignInResponse(Request $request)
    {
        return back()->withInput($request->only('identifier', 'remember'))->withErrors(['loginfailed' => Lang::get('auth.failed')]);
    }

    protected function getCredentials(Request $request)
    {
        $field = ['identifier' => filter_var($request->input('identifier'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username', 'password' => 'password'];
        $request->merge([$field['identifier'] => $request->input('identifier'), $field['password'] => $request->input('passwordL')]);
        return $request->only($field);
    }
}

RegisterUsers.php

<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use Illuminate\Http\Request;

trait RegisterUsers
{
    public function postSignup(Request $request)
    {
        $validate = $this->validaterSignUp($request);
        User::createUser($request->only('username', 'email', 'passwordR'));
        return $this->sendFailedSignUpResponse();
    }

    protected function validaterSignUp(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|min:3|max:50|unique:users,username',
            'email' => 'required|email|min:3|unique:users,email',
            'passwordR' => 'required|min:3|max:50',
            'confirm_password' => 'required|min:3|max:50|same:passwordR',
        ]);
    }

    protected function sendFailedSignUpResponse(Request $request)
    {
        return back()->withInput($request)->withErrors(['RegisterUserModal' => '#RegisterUserModal']);
    }
}

that is all the changes i did.

SaeedPrez's avatar

@kaxias try this..

    protected function sendFailedSignUpResponse(Request $request)
    {
        return back()->withInput($request->all())->withErrors(['RegisterUserModal' => '#RegisterUserModal']);
    }

Change: $request->all()

    public function postSignup(Request $request)
    {
        $validate = $this->validaterSignUp($request);
        User::createUser($request->only('username', 'email', 'passwordR'));
        return $this->sendFailedSignUpResponse($request); // <<-- added $request
    }
nickywest's avatar

Possibly unrelated but, you're not passing $request to sendFailedSignUpResponse() in RegisterUsers::postSignup

return $this->sendFailedSignUpResponse();
// should be 
return $this->sendFailedSignUpResponse($request);
SaeedPrez's avatar

@nickwest yeah, I agree. Noticed that as well.

Kind of unrelated but your route group does basically nothing..

// Route::group(['prefix' => '/'], function ($route) {
// });

Route::get('/', ['as' => 'home', 'uses' => 'HomeController@home']);

// doesn't matter if you add / in front or not
Route::post('signin', ['as' => 'signin', 'uses' => 'Auth\AuthController@postSignin']);
Route::post('/signup', ['as' => 'signup', 'uses' => 'Auth\AuthController@postSignup']);
Route::get('signout', ['as' => 'signout', 'uses' => 'Auth\AuthController@signOut']);

// I personally prefer, but again.. does not matter :)
Route::get('signout', 'Auth\AuthController@signOut')->name('signout');
ABasso's avatar
Level 34

It was a nightmare but is now working.

    public function postSignup(Request $request)
    {
        $validate = $this->validaterSignUp($request);
        if($validate->fails()) {
            return $this->sendFailedSignUpResponse($request, $validate);
        }
        return redirect()->intended();
    }

    protected function validaterSignUp(Request $request)
    {
        return Validator::make($request->all(), [
            'username' => 'required|min:3|max:50|unique:users,username',
            'email' => 'required|email|min:3|unique:users,email',
            'passwordR' => 'required|min:3|max:50',
            'confirm_password' => 'required|min:3|max:50|same:passwordR',
        ]);
    }

    protected function sendFailedSignUpResponse(Request $request, $validate)
    {
        return back()->withInput($request->all())->withErrors($validate)->with('RegisterUserModal', '#RegisterUserModal');
    }
ABasso's avatar
Level 34

thank you friends for the help. from the bottom of my heart

Please or to participate in this conversation.