在laravel控制器中,将变量传递给一个功能给另一个功能

时间:2019-05-01 06:41:09

标签: php laravel eloquent controller

我正在Laravel中进行编码,如何将变量从一个函数传递给Controller中的另一个函数,

在控制器文件中,我有2个这样的功能

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('hrm@wcg.com', 'HRM');
        $message->to($mail);
        $message->attach('uploads/cv/'.$name);
    });

    return redirect('list-candidate');
}

我想从$candidateName函数的$name函数中使用assignInterview()hiringEmployee()

我该怎么办?

3 个答案:

答案 0 :(得分:3)

您将无法直接从其他函数中使用$name$candidateName,因为它们看起来像是针对两个不同的请求,但是,看起来您正在保存该数据当您在EmployeeHire方法中创建新的hiringEmployee()时访问数据库,因此您应该已经可以在assignInterview()方法中访问该信息:

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

$candidateName = $assignInterview->candidateName;
$name  = $assignInterview->file;

答案 1 :(得分:1)

根据您的情况,您可以使用两种方法:

#1

使用会话变量,如下所示:

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

然后:

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

#2

使用类属性:

class acontroller extends Controller
{    
    private $classCandidateName;

}

答案 2 :(得分:0)

您可以尝试以下操作:

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('hrm@wcg.com', 'HRM');
        $message->to($mail);
        $message->attach('uploads/cv/'.$assignInterview->file);
    });

    return redirect('list-candidate');
}

请,在使用find($ id)时要小心。如果为空,则会出现错误。

玩得开心!