amin devloper
amin devloper

Reputation: 100

In laravel in controller pass variable to one function to another function

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?

Upvotes: 0

Views: 1656

Answers (3)

Viruscom1000
Viruscom1000

Reputation: 119

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!

Upvotes: 0

Mohsen Shakibafar
Mohsen Shakibafar

Reputation: 254

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;

}

Upvotes: 2

Rwd
Rwd

Reputation: 35170

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;

Upvotes: 3

Related Questions