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

alya_alsiyabi's avatar

How to store variable in session to use it in different function in controller laravel8

Hi I am trying to store job_id (from public function applyforjob )inside session in order to use it in public store function. So I add this line :

 use Illuminate\Support\Facades\Session;

then I add inside public function applyforjob session as following:

  public function applyforjob(Request $request ,$id)
     {
 
 $jobs = Job::find($request->job_id);
        Session::put('$request->job_id');
      $user = User::find(auth()->user()->id);
     $cv = auth()->user()->cv;
    $cv=cv::all();
              
      }

This job_id i want to use in following function :

 public function store (Request $request ){
    
    if ($cv->save()) {
        $application = new JobApplication;

        $c=Session::get('$request->job_id');
        $application->job_id = $request->job_id;
dd($application);
        $user = User::find(auth()->user()->id);
        $application->user_id = auth()->user()->id;
        $cv = auth()->user()->cv;
        $i= $cv->id;
        $application->cv_id = $i;  
        $application->save();
        $request->session()->forget('$request->job_id'); 
         return true;
    }

Now the the job_id is not recognized in the second function and give me following error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'job_id' cannot be null (SQL: insert into job_applications (job_id, user_id, cv_id, updated_at, created_at) values (?, 3, 145, 2022-03-15 18:19:34, 2022-03-15 18:19:34))

This is due to the null value of job_id enter image description here

0 likes
8 replies
Sinnbeck's avatar

You need to give it a key and a value. Example

Session::put('job_id', $request->job_id); //set
$c=Session::get('job_id'); //get
alya_alsiyabi's avatar

@Sinnbeck key is id ...but the values are not fixed i mean it change according to job_id sometime 1 sometimes 2

Sinnbeck's avatar

@alya_alsiyabi you still need a value

Session::put('job_id'. $request->job_id, $value); //set
$c=Session::get('job_id'. $request->job_id); //get 
alya_alsiyabi's avatar

@Sinnbeck Like what can be the value ? example: name is key John is value but in my case $request->Job_id; is key but the values depands on this request->Job_id so can i use post[$request->Job_id]? or i can not use Session in my case ?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@alya_alsiyabi I am not sure what you are saying. But it sounds like you are misunderstanding how sessions work. You cannot just store a job id by itself. My first example was most likely what you wanted, but i am not sure..

You can look at the session like a hidden input in a form

<input type="hidden" name="job_id" value"{$job_id}" />

Would be somewhat the same as

Session::put('job_id', $job_id); 

and getting it out again with input

$request->input('job_id');

and session

Session::get('job_id');
2 likes
Snapey's avatar

have you actually considered your code

  public function applyforjob(Request $request ,$id)
     {
 
 $jobs = Job::find($request->job_id);
// you use 'jobs' but find only returns a single job, but then you never use $jobs

        Session::put('$request->job_id');
// needing to save this to session is a sign that you have not thought the application through well enough

      $user = User::find(auth()->user()->id);
// you realise that you are just getting the user that you already have ?

     $cv = auth()->user()->cv;
// not using $ user that you just resolved

    $cv=cv::all();
// and then immediately overwriting $cv making the previous line pointless

// and then just ending the function without returning anything..... What was the point?
              
      }

your code is functionally equivalent to

      public function applyforjob(Request $request ,$id)
     {
         Session::put('$request->job_id');          
      }

Which won't work because as you have been advised, you need to save a key and a value

1 like
alya_alsiyabi's avatar

@Snapey Like what can be the value ? example: name is key John is value but in my case $request->Job_id; is key but the values depands on this request->Job_id so can i use post[$request->Job_id]? or i can not use Session in my case ?

**Sir this one is not my entire code

alya_alsiyabi's avatar

I used hidden value variable instead of session to store id variable through blade and request ->id .

Please or to participate in this conversation.