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

mozew's avatar
Level 6

How to get id in session for next page?

I have an order and I want to after save, get in session current id of order for next page reports.

use Session;

public function store(Request $request)
{
            $order = new Order($request->all());
            $order->user_id = auth()->user()->id;
            $order->title = $request->title;
            $order->body = $request->body;
            $order->id = $request->session()->get('id');
            $order->description = $request->description;
            $order->save();
            session(['order_id' => $order_id]);
            return redirect()->route('reports.index')->with('order_id', $request->id);
}

In notes page has a input hidden for get session of article id.

     <input type="hidden" class="form-control" value="{{ Session::get('order_id') }}" name="id" id="id">

But I see input hidden. It is blank . "".

0 likes
8 replies
Cronix's avatar

If you can login and stay logged in then sessions are working, or else you'd have to login on every page.

bastikoe's avatar

Your

$order_id

was never declared in your code snippet. Try to use

$order->id

instead. Also, I think your

->with('order_id', $request->id); 

in this example is unnecessary because you get the order id in your template from the session variable. You don't need to pass the ID again, as the $request->id is not your id of the newly created order.

Snapey's avatar

try actually saving the $order->id; (not $order_id)

session(['order_id' => $order->id]);

And check ALL your code. It is really bad.

You create a new Order model and fill it with data

Then you individually set order attributes

Then you try to set the ID of the order you are creating with something from the request

1 like
jlrdw's avatar

NO.

Then Step One is troubleshoot why and get the sessions working.

Snapey's avatar

This code is probably not actually being ran because there is no $order_id so this would throw an error

Please or to participate in this conversation.