BadMethodCallException方法Illuminate \ Database \ Query \ Builder :: input不存在

时间:2019-03-07 06:20:48

标签: laravel

提交表单数据进行存储时出现错误,这是我在控制器中的approve_request_post函数。

public function approve_request_post(Request $request, $request_hash)
{
    $request->validate([
    'hosp_no' => 'required',
    'transport_cost' => 'required',
    'days' => 'required|numeric',
    'per_diem' => 'required|numeric',
    'service_type' => 'required',
    'trans_mean' => 'required',
    'cost_payable' => 'required|numeric',
    'patient_age' => 'required|numeric',
    'doctors_name' => 'required',
    'appointment_date' => 'required|date',
    'comment' => 'required',
    ]);


    // Start transaction
    DB::beginTransaction();


    $request = ReferralRequestModel::where('request_hash', $request_hash)->firstOrFail();

    $remark = new InsurerRemarksModel;
    $remark->ir_hash = encrypt($remark->ir_id);
    $remark->req_id = $request->request_id;
    $remark->insurer_id = Auth::user()->insurers->insurer_id;
    $remark->req_id = $request->request_id;
    $remark->hosp_no = $request->input('hosp_no');
    $remark->service_type = $request->input('service_type');
    $remark->transport_cost = $request->input('transport_cost');
    $remark->trans_mean = $request->input('trans_mean');
    $remark->days = $request->input('days');
    $remark->cost_payable = $request->input('cost_payable');
    $remark->patient_age = $request->input('patient_age');
    $remark->doctors_name = $request->input('doctors_name');
    $remark->appointment_date = $request->input('appointment_date');
    $remark->approval_date =Carbon::now();
    $remark->ir_status = 'approved';
    $remark->save();

    //approvalrecord
    $approval = new ApprovalModel;
    $approval->req_id = $request->request_id;
    $approval->approver_id = Auth::user()->id;
    $approval->category = 'Insurer | Verified By: ';
    $approval->status = 'Verified';
    $approval->comment = $request->input('comment');
    $approval->save();

    //email to all medical team

    if( !$remark->save() || !$approval->save() )
    {
        DB::rollback();
        return back()->withInput(Input::all())->with('failure', 'Transaction Not Successful. Check the input data');
    }

    DB::commit();

    return redirect('/insurer-view-submitted-requests')->with('success', 'Referral Request Approved  Successfully');
}

1 个答案:

答案 0 :(得分:2)

替换此行

$referral_model = ReferralRequestModel::where('request_hash', $request_hash)->firstOrFail();

因为您要用模型实例替换$request并尝试使用$request->input('hosp_no')这样的值来获取值

$request->input('hosp_no'),该方法将尝试从您的ReferralRequestModel

获取输入方法

因此替换上面的行,并在需要的地方使用$referral_model

还建议使用trycatch块来处理异常。因为如果找不到数据,firstOrFail会抛出Illuminate\Database\Eloquent\ModelNotFoundException异常