0

I am coding in Laravel, How can I pass variable to one function to another function in Controller,

In controller file I have 2 functions like this

public function hiringEmployee(Request $request)
{
    $hireEmployee = new EmployeeHire();
    $hireEmployee->candidateName = $request->get('candidateName');

    $file = $request->file('file');
    $name = $file->getClientOriginalName();
    $file->move('uploads/cv', $name);

    $hireEmployee->file = $name;
    $hireEmployee->save();

    return redirect('list-candidate');
}


public function assignInterview(Request $request, $id)
{
    $assignInterview = EmployeeHire::find($id);
    $interview = $request->get('interview');
    $assignto = $request->get('assignto');
    $dateTime = $request->get('dateTime');
    $note = $request->get('note');

    $interviewDetails = ([
        'interview' => $interview,
        'assign_to' => $assignto,
        'date_time' => $dateTime,
        'note'      => $note,
    ]);

    $assignInterview->interview_details = $interviewDetails;
    $assignInterview->save();


    Mail::send('emails.hireemployee', ['candidateName' => $candidateName], function ($message) use ($assignto, $name) {
        $message->subject('Interview For New Candidate!');
        $message->from('[email protected]', 'HRM');
        $message->to($mail);
        $message->attach('uploads/cv/'.$name);
    });

    return redirect('list-candidate');
}

I want to use $candidateName and $name in assignInterview() function from hiringEmployee() function.

How can I do it?

1
  • If one of the below posts answered your question please may you mark it as accepted :) Thanks. Commented Jan 18, 2020 at 12:43

3 Answers 3

3

You won't be able to use the $name and $candidateName directly from the other function as they look like they are for two different requests, however, it looks like you're saving that data to database when you're creating a new EmployeeHire in your hiringEmployee() method so you should already have access to that information in your assignInterview() method:

$assignInterview = EmployeeHire::find($id); // this is where you loading the model

$candidateName = $assignInterview->candidateName;
$name  = $assignInterview->file;
Sign up to request clarification or add additional context in comments.

Comments

2

In your situation , you can use two approach:

#1

Use Session Variable as below:

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

Then:

$value = Session::get('candidateName');

#2

Use class attribute:

class acontroller extends Controller
{    
    private $classCandidateName;

}

2 Comments

And I wrote my whole assignInterview() function in acontroller class???
In your Controller that you have two functions, add two attribute into your class, after that you can call and use the value in attribute. if you want to call function another time, you can retrieve data from your database and use them.
0

You can try something like this:

public function hiringEmployee(Request $request)
{
    $hireEmployee = new EmployeeHire();
    $hireEmployee->candidateName = $request->get('candidateName');

    $file = $request->file('file');
    $name = $file->getClientOriginalName();
    $file->move('uploads/cv', $name);

    $hireEmployee->file = $name;
    $hireEmployee->save();

    return redirect('list-candidate');
}


public function assignInterview(Request $request, $id)
{
    $assignInterview = EmployeeHire::find($id);
     if(is_null($assignInterview)){
         return redirect()->back()->withErrors(['Error message here']);
     }

    $interviewDetails = ([
        'interview' => $request->get('interview'),
        'assign_to' => $request->get('assignto'),
        'date_time' => $request->get('dateTime'),
        'note'      => $request->get('note'),
    ]);

    $assignInterview->interview_details = $interviewDetails;
    $assignInterview->save();


    Mail::send('emails.hireemployee', ['candidateName' => $assignInterview->candidateName], function ($message) use ($assignto, $assignInterview->file) {
        $message->subject('Interview For New Candidate!');
        $message->from('[email protected]', 'HRM');
        $message->to($mail);
        $message->attach('uploads/cv/'.$assignInterview->file);
    });

    return redirect('list-candidate');
}

Please, you should to be careful with find($id). If it is a null, you will get an error.

Have fun!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.