Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danultimate's avatar

getting undefined index with a post.

I'm trying to fetch some data in a table, GET requests work ok but when I try to add a new column from the form.

Testing with postman gives me Undefined index: proveedor

This is my controller code:

public function store(Request $request)
{
    $validator = Validator::make(
        $request->all(),
        [

        ]
    );

    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()], 403);
    } else {
        $params = $request->all();
        $suppliers = Suppliers::create([
            'proveedor' => $params['proveedor'],
            'code' => strtolower($params['proveedor']) . time(), // Just to make sure this value is unique
            'contacto' => $params['contacto'],
            'telefono' => $params['telefono'],
            'email' => $params['email'],
            'pais' => $params['pais'],
            'direccion' => $params['direccion'],
            'calidad' => $params['calidad'],
            'pedidos' => $params['pedidos'],
        ]);
        
        return new SuppliersResource($supplier);
    }
}
0 likes
15 replies
tykus's avatar

It would seem that there is no proveedor field in the submitted data. You can temporarily dump the request payload to see what you are receiving.

public function store(Request $request)
{
    dump($request->all());

    $validator = Validator::make(
        $request->all(),
        [

        ]
    );

    // etc...

It probably is not appropriate in this case, but more generally, you can mitigate these types of errors and exceptions using the data_get() helper, which allows for setting a default value in the event that there is no key:

data_get($request->all(), 'proveedor', null);
danultimate's avatar

Thanks for your reply.

with the dump($request->all()); Now Im able make a post but the data isnt saved or displayed.

tykus's avatar

You missed my point above, what do you see in the dumped request data - is there a proveedor key in the array?

danultimate's avatar

Yes. {"data":[{"id":1,"proveedor":"OGmv6PfuBO","marca":"N8IojsEYMc","contacto":"RuagUqRhP0","telefono":1463235513,"email":"xIPTD221m6@gmail.com","pais":"vda9eFPoOY","direccion":"SQFZdYJ0sP","calidad":"RoMKm9HB72","pedidos":"foL8W9iLup"},{"id":2,"proveedor":"dWHZKInbOW","marca":"GooUCnerT3","contacto":"pAJkPcMr8G","telefono":1572260665,"email":"[email protected]","pais":"j8Vvy4Hoe2","direccion":"501tS7jd0p","calidad":"FGz2U0hXng","pedidos":"hWLgDSzVsC"}]}

tykus's avatar

So, your payload is an array nested under a data key, whereas your code expects to create a single Supplier resource with the parameters to do so such as proveedor (and others) to be at the top level.

So the question is; do you want to create one or more than one Supplier?

tykus's avatar

Ok that is not achievable from your current code; you need to iterate over this incoming request data creating a Supplier for each set of data:

public function store(Request $request)
{
    $validator = Validator::make(
        $request->all(),
        [
            // ensure that the validation rules expects the data in the format you have dumped above
        ]
    );
    
    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()], 403); // 422 is more appropriate
    }
    
    $suppliers = collect($request->input('data'))->map(function($params) {
        return Suppliers::create([
            'proveedor' => $params['proveedor'],
            'code' => strtolower($params['proveedor']) . time(), // Just to make sure this value is unique
            'contacto' => $params['contacto'],
            'telefono' => $params['telefono'],
            'email' => $params['email'],
            'pais' => $params['pais'],
            'direccion' => $params['direccion'],
            'calidad' => $params['calidad'],
            'pedidos' => $params['pedidos'],
        ]);
    });

    return new SuppliersResource($suppliers);
}

Here you are putting the contents of the $request->input('data') into a collection so that we can create the Suppliers collection to pass to the SupplierResource

tykus's avatar

Well, you have said it yourself you are sending a POST request in Postman, but a PUT request in the browser; remove the _method field from the form - this might be a @method('PUT') directive - it is unnecessary. Ensure the form method attribute**is**POST`

danultimate's avatar

Thanks again but my form don't have a method:

<el-dialog :title="formTitle" :visible.sync="supplierFormVisible">
  <div class="form-container">
    <el-form ref="supplierForm" :model="currentSupplier" label-position="left" label-width="150px" style="max-width: 500px;">
      <el-form-item label="Proveedor" prop="proveedor">
        <el-input v-model="currentSupplier.proveedor" />
      </el-form-item>
      <el-form-item label="Marca" prop="marca">
        <el-input v-model="currentSupplier.marca" />
      </el-form-item>
      <el-form-item label="Contacto" prop="contacto">
        <el-input v-model="currentSupplier.contacto" />
      </el-form-item>
      <el-form-item label="Telefono" prop="telefono">
        <el-input v-model="currentSupplier.telefono" />
      </el-form-item>
      <el-form-item label="Email" prop="email">
        <el-input v-model="currentSupplier.email" />
      </el-form-item>
      <el-form-item label="Pais" prop="pais">
        <el-input v-model="currentSupplier.pais" />
      </el-form-item>
      <el-form-item label="Direccion" prop="direccion">
        <el-input v-model="currentSupplier.direccion" />
      </el-form-item>
      <el-form-item label="Calidad" prop="calidad">
        <el-input v-model="currentSupplier.calidad" />
      </el-form-item>
      <el-form-item label="Pedidos" prop="pedidos">
        <el-input v-model="currentSupplier.pedidos" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="supplierFormVisible = false">
        Cancel
      </el-button>
      <el-button type="primary" @click="handleSubmit()">
        Confirm
      </el-button>
    </div>
  </div>
</el-dialog>
tykus's avatar

I don't use element-ui myself, is there a handler method (e.g. onSubmit) for the form where the form submission is handled, and a request verb/method specified?

tykus's avatar

You'll need to work out why a PUT request is being submitted; or replace the Vue component with something you can grok more easily.

danultimate's avatar

Someone told me that specify the method will be the solution but still getting the error.

<el-form ref="supplierForm" :model="currentSupplier" label-position="left" label-width="150px">

danultimate's avatar

Noticed that the put comes from the api/resources.js file.

update(id, resource) { return request({ url: '/' + this.uri + '/' + id, method: 'post', data: resource, }); }

If I change the method to post, I don't get the error but don't update or create the new supplier.

Please or to participate in this conversation.