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

HarishKarthick's avatar

Laravel Store and retrieve session using ajax call

I am trying to create a session and retrieve it and get db values using the session id

(i.e) While on click edit i like to store the data-id in session and from session id i like to retrieve the db values using Eloquent

so far i stored and retrieve the session the only problem i dont know to get db from that session id here is my folllowing code

My ajax call : $(document).ready(function(){ $('.editaction').click(function(){ var editaction=($(this).attr("data-id")); console.log(editaction); $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $.ajax({ type:"POST", url:"/design_session", data:"design_id="+editaction, success:function(results){ window.location.href="/design_edit"; } }); }); })

Db controller page :

public function show()
{
    $design_id = $request->session()->get('design_id');
    return view('pages.design_edit')->with('design',$design_id);
}

so far i am getting redirect to another page successfully but i don't how to invoke or pass the ajax through the function to get db values

0 likes
4 replies
HarishKarthick's avatar

I am it self solved this problem here is solved code

public function show(Request $request)
{
    $design_id = $request->session()->get('design_id');
    $designView = design::find($design_id);
    return view('pages.design_edit')->with('design',$designView);
}
Snapey's avatar

why on earth...

you ajax post a variable and save it in session just so that you can then redirect to a page and use that variable?

why don't you just put the design_id on the edit url like everyone else would do?

1 like
HarishKarthick's avatar

Lets think different...

I don't want to expose the id in URL and POST method is more secure than GET method .

Snapey's avatar

Obscurity is a poor way to think about security.

Please or to participate in this conversation.