关于如何将对象的ID获取到我的create方法,是否有任何有效的方法

时间:2019-01-22 04:32:58

标签: laravel model-view-controller

我正在创建一个Web模块,并且想将表许可级别2的ID解析到我的create方法中。因此,每个级别的ID都会有一个任务,并且该ID需要作为外键存储在我的许可表中,该外键反映了Level 2表中的ID。我该如何解决呢,任何人都可以给我一个很好的建议或方法

 public function add_show($id) 
    { 
        $level = PreLicensingLevelTwo::where('id', $id)->first(); 
        $level->prelicensingtask  = PreLicensingTask::where('pre_licensing_level_two_id', $level->id)->with('staff', 'statusdesc', 'prelicensingtaskstaff')->get();
        return view('staff.regulatory.statutory.approval.display',compact('level')); 
    }

    public function create()
{   
    $staff = Staff::pluck('staff_name'); 
    $status = PreLicensingStatus::pluck('status_description', 'id');
    return view('staff.regulatory.statutory.approval.create', compact('staff','status'));
}

public function show($id)
{
     $one = PreLicensingLevelOne::where('pre_licensing_main_id', $id)->get();
     foreach ($one as $key => $license) 
     {
        $license->two = PreLicensingLevelTwo::where('pre_licensing_level_one_id', $license->id)->get();
     }
     $rendered = view('staff.regulatory.statutory.approval.show')->with('one', $one)->render();
     return response()->json(array('status' => 1, 'tableData' => $rendered));    
}

1 个答案:

答案 0 :(得分:0)

  

在我的工作同事的帮助下,这就是我能够解决所问问题的方法

public function store(Request $request)
{
     $this->validate($request, [
        'task_title' => 'required', 
        'task_description' => 'required', 
        'task_due_date' => 'required', 
     ]);

     $leveltwo = PreLicensingLevelTwo::find($request->input('pre_licensing_level_two_id'));

     $prelicensingtask = new PreLicensingTask; 
     $prelicensingtask->task_title =$request->input('task_title'); 
     $prelicensingtask->task_description =$request->input('task_description'); 
     $prelicensingtask->task_due_date =$request->input('task_due_date');
     $prelicensingtask->created_by_staff_id = Auth::user()->ref_user->staff_id;
     $prelicensingtask->status = $request->input('status');
     $prelicensingtask->pre_licensing_level_two_id = $leveltwo->id;
     $prelicensingtask->pre_licensing_level_one_id = $leveltwo->pre_licensing_level_one_id;
     $prelicensingtask->pre_licensing_main_id = $leveltwo->pre_licensing_main_id;
     $prelicensingtask->centre_id = Auth::user()->ref_user->centre_id;
     $prelicensingtask->save(); 
     return redirect()->back();

}