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

anujsharma9196's avatar

settings session flash without $request

I have a action in UserController which accepts only $id

public function makeAdmin($id)
    {
        $user = DB::table('users')->find($id);

        $user->admin = 1;
        if ($user->save()) {
            $request->session()->flash('message.level', 'success');
            $request->session()->flash('message.content', 'User assigned admin role');
        } else {
            $request->session()->flash('message.level', 'danger');
            $request->session()->flash('message.content', 'could not be assigned admin role.');
        }

        return back();
    }

But here, $request is not defined as I have not passed Request $request in the function parameter.

I have defined routes/web.php as

Route::get('User/makeAdmin/{id}', [
        'middleware' => 'auth',
        'uses' => 'UserController@makeAdmin'
]);

Question

  1. How can I make flash session without $request?
  2. If I need to include Request $request in parameter, How to put it in route?
0 likes
3 replies
tisuchi's avatar

1

In your method, just add following session.

Session::flash('message', 'This is a message!'); 

In your next page (where you need to redirect), just check as follows-

@if(Session::has('message'))
<p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>
@endif

Ref: https://stackoverflow.com/questions/21004310/in-laravel-the-best-way-to-pass-different-types-of-flash-messages-in-the-sessio

2

I did't get your second question properly. If you mean, how can you get url parameter value, you just try like that-

Request::segment()

Imagine that, you have a url http://example.com/param1/param2/param3 Now from this url, if you need to get "param3", just use

Request::segment(3);

Ref: https://laravel.com/api/5.0/Illuminate/Http/Request.html

AddWebContribution's avatar

Try this simple way:

In UserController:

public function makeAdmin($id)
{
    $user = DB::table('users')->find($id);

    $user->admin = 1;
    if ($user->save()) {
        Session::flash('success', 'User assigned admin role');
    } else {
        Session::flash('danger', 'could not be assigned admin role.');
    }

    return back();
}

In blade

<div class="flash-message">
    @foreach (['danger', 'success'] as $msg)
        @if(Session::has($msg))
            <p class="alert alert-{{ $msg }}">{{ Session::get($msg) }}</p>
        @endif
    @endforeach
</div>

That's it...

Snapey's avatar

you can

a) pass request into the controller

b) use request() helper or facade

c) use session() helper or facade

d) return withMessage()

to pass request, you don't need to add it into the route. It will be resolved by the IOC container

public function makeAdmin(Request $request, $id)

By the way, I hope you have this route locked down. I would love to swing by your site and make every user an admin

Please or to participate in this conversation.