skip to Main Content

i want to loop through the multiple input fields on html blade so as to be able to upload them to my database using the laravel query builder. But the problem here is that its telling me:"Attempt to read property "drugid" on string"

lets assume this is one of the html inputs:

<input type="text" name="drugname[]">
<input type="text" name="drugname[]">
<input type="text" name="drugname[]">

Next was on my controller where i looped the fields in order for it to upload to the database

 public function prescribedrugs(Request $presdrug){
    $validator = Validator::make($presdrug->all(), [
        'consultclinic' => 'required|string',
        'drugdiagnosis' => 'required|string',
        'drugpassword' => 'required|string',
    ]);
    $getuser = new Getloggedinuserservice();
    $getloggedinuser = $getuser->getuser();
    if ($validator->fails()) {
        return response()->json([
        'error' => $validator->errors()->all()
        ]);
    }else{
        
        $lastapptid = DB::table('AllDrugPresc')->max('Presc_Num');
        $newid = $lastapptid + 1;
        foreach($presdrug->drugname as $key => $d){
            return DB::table('AllDrugPresc')
            ->insert([
                'Presc_Num'=>$newid,
                'Date'=>Carbon::now(),
                'Time'=>date('H:i:s'),
                'Drug_Id'=>$presdrug->drugid[$key],
                'Drug_Name'=>$d,
                'Prescription'=>$presdrug->drugprescription[$key],
                'Hospital_No'=>'44455eee',
                'Surname'=>'rrr',
                'Other_Name'=>'rrr',
                'Age'=>44,
                'Sex'=>'ee',
                'Account'=>'ee',
                'Account_Category'=>'ddd',
                'Clinic'=>$presdrug->consultclinic,
                'Diagnosis'=>$presdrug->drugdiagnosis,
                'Prescribed_By'=>'wwwe',
                'Qty_given'=>2,
                'Total_Cost'=>33
            ]);
        }
    }
}

Can someone tell me what i am doing wrong please. Thanks in advance

2

Answers


  1. Because $presdrug->drugid is a string type, you can not use foreach. One more thing, you are using the undefined variable $newId. Try the code below:

    //....
    $lastapptid = DB::table('AllDrugPresc')->max('Presc_Num');
    
    foreach($presdrug->drugname as $key => $d){
        $newid = $lastapptid + ($key + 1);
        
    //...
    
    Login or Signup to reply.
  2. First of all make a validated class that will validate the inputs by running php artisan make: request DrugValidationRequest

    class DrugValidationRequest extends FormRequest
    {
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            return [
                'consultclinic' => 'required|string',
                'drugdiagnosis' => 'required|string',
                'drugpassword'  => 'required|string',
            ];
        }
    
        public function messages()
        {
            return [
                'consultclinic.required' => 'Give your required message',
                'drugdiagnosis.required' => 'Give your required message',
                'drugpassword.required'  => 'Give your required message',
            ];
        }
    }
    

    Then do the following code. Don’t insert the data using a loop. in stead of making data into loop and then insert directly

     public function prescribedrugs(DrugValidationRequest $presdrug)
    {
        $getUser         = new Getloggedinuserservice();
        $getLoggedInUser = $getuser->getuser();
        $lastapptid      = DB::table('AllDrugPresc')->max('Presc_Num');
        $newid           = $lastapptid + 1;
        $paramData       = [];
    
        foreach ($presdrug->drugname as $key => $d) {
            $paramData[] = [
                'Presc_Num'        => $newid,
                'Date'             => Carbon::now(),
                'Time'             => date('H:i:s'),
                'Drug_Id'          => $presdrug->drugid[$key],
                'Drug_Name'        => $d,
                'Prescription'     => $presdrug->drugprescription[$key],
                'Hospital_No'      => '44455eee',
                'Surname'          => 'rrr',
                'Other_Name'       => 'rrr',
                'Age'              => 44,
                'Sex'              => 'ee',
                'Account'          => 'ee',
                'Account_Category' => 'ddd',
                'Clinic'           => $presdrug->consultclinic,
                'Diagnosis'        => $presdrug->drugdiagnosis,
                'Prescribed_By'    => 'wwwe',
                'Qty_given'        => 2,
                'Total_Cost'       => 33
            ];
        }
    
        return DB::table('AllDrugPresc')->insert($paramData);
    
    }
    

    Also in the handler add the validation rule too.

     public function render($request, Throwable $e)
    {
        if ($e instanceof ValidationException) {
            if ($request->ajax()) {
                return response()->json($e->validator->getMessageBag()->toArray());
            }a
            return redirect()->back()->withErrors($e->validator->getMessageBag()->toArray())->withInput();
        }
     
       throw new IlluminateValidationValidationException($validator);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search