skip to Main Content

I make a dropdown for a form, I will show the code below. However, when I click the submit button, there is an error saying,
SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘brand’ cannot be null (SQL: insert into supplier_details.
The data that I chose from the dropdown is actually null. Actually, I’m new to Laravel.
I don’t want to make a dropdown list from a database, I just want to display the option and the option will be inserted into the database when the user clicks the submit button after filling in the form.

    <div class="form-group row">
            <label style="font-size: 16px;" for="id" class = "col-sm-2">Item Brand </label>
            <label for="supp_name" class = "col-sm-1">:</label>
                <div class="col-sm-7">
                    <select name="brand" class="form-control js-example-basic-single" required>
                    <option >Please select item brand</option>  
                    <option value="machine1"> Item Brand 1 </option>  
                    <option value="machine1"> Item Brand 2 </option>  
                    <option value="machine1"> Tem Brand 3 </option>  
                    </select>
                </div>  
         </div>

Controller

public function createsupplierdetails()
        {
            return view ('frontend.praiBarcode.getweight');
        }

    public function supplierdetails(Request $r)
    {

       $details = new SupplierDetail;
       $getuserPO = Supplier::where('PO',$r->PO)->first();

        $details->brand = $getuserPO->brand;
        $details->container_no = $getuserPO->container_no;
        $details->date_received = $getuserPO->date_received;
        $details->gross_weight = $getuserPO->gross_weight;
        $details->tare_weight = $getuserPO->tare_weight;
        $details->net_weight = $getuserPO->net_weight;
        $details->save();

        return view ('frontend.praiBarcode.viewsupplierdetails')
        ->with('details',$details);
        
        }

2

Answers


  1. This to check to verify if it is working:

    1. Make sure you are submitting the correct form.
    2. Try doing dd on your controller dd($request->all())
    3. If data is reaching the controller and not inserted into the database, check on your model, if it is added to fillable or if there is only id in the guarded array. You can know about more here in https://laravel.com/docs/9.x/eloquent#mass-assignment

    Error should be fixed, as soon as you fix it.

    Login or Signup to reply.
  2. Controller
    use Validator;

      public function store(Request $request)
    
      {
        $validator = Validator::make($request->all(), [
    
            'brand' => 'required',
            
        ]);
    
        if ($validator->fails()) {
            return redirect()->back()->with('error', $validator->errors()->first());
        }
        $details = new SupplierDetail();
    
        $details->brand = $request->brand;
        $details->container_no = $request->container_no;
        $details->date_received = $request->date_received;
        $details->gross_weight = $request->gross_weight;
        $details->tare_weight = $request->tare_weight;
        $details->net_weight = $request->net_weight;
        $details->save();
        if ($trending) {
            return redirect(route('details.index'))->with('success', 'Field added successfully');
        } else {
            return redirect()->back()->with('error', 'Field has been not added successfully');
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search