Laravel存储到数据库中(错误为空值)

时间:2018-08-02 09:46:29

标签: php laravel

我想获取咨询ID并将其输入到drug_prescription表中。我已经用回声测试了$ consultation_id变量,并且其中有一个正确的值。但是我无法将该变量输入到drug_prescription表中。

下面是控制器的代码:

public function store(Request $request)
{

    $this->validate($request,[
        'client_id' => 'required',
        'staff_id' => 'required',
        'diagnosis' => 'required',
    ]);

    //Create Post
    $consultation = new consultation;
    $consultation->client_id = $request->input('client_id');
    $consultation->staff_id = $request->input('staff_id');
    $consultation->client_complaints = $request->input('client_complaints');
    $consultation->observations = $request->input('observations');
    $consultation->physical_abnormalities = $request->input('physical_abnormalities');
    $consultation->diagnosis = $request->input('diagnosis');
    $consultation->remarks = $request->input('remarks');
    $consultation->save();

$client_id = $request->client_id;

    $consultation = DB::table('consultations')
    ->where('client_id', '=', $client_id)
    ->orderBy('created_at', 'desc')
    ->take(1)
    ->get();

    // var_dump($consultation);

    $client_id = $request->client_id;
    $consultation_id = $consultation[0]->id;

    $count = $request->count;
    for($i=0;$i<$count;$i++){
        $quantity = 'quantity' . $i;
        $quantityStatus = $request->$quantity;
        if($quantityStatus!=NULL){
            $drug_prescription = new drug_prescription;
            $drug_prescription->client_id = $request->input('client_id');
            $drug_prescription->drug_id = $request->input('drug' . $i);
            $drug_prescription->consultation_id = $request->input($consultation_id) ;
            $drug_prescription->amount_perday = $request->input('amount_perday' . $i);
            $drug_prescription->amount_ofday = $request->input('amount_ofday' . $i);
            $drug_prescription->quantity = $request->input('quantity'. $i);
            $drug_prescription->price = $request->input('price'. $i);
            $drug_prescription->save();
        }
    }
    // return redirect('/appointment')->with('success', 'Consultation Infomation Added');
    // dd($request->all());
}

2 个答案:

答案 0 :(得分:1)

$drug_prescription->consultation_id = $request->input($consultation_id) ;应该是

$drug_prescription->consultation_id = $consultation_id;

因为它的值不是来自request的输入,而是来自您的代码。

答案 1 :(得分:0)

$drug_prescription->consultation_id = $request->input($consultation_id) ;

$drug_prescription->consultation_id = $consultation_id;

我希望它能起作用。