Undefined offset: 1 laravel

i’m new to laravel
I have a form with dynamic fields but when I create more than one dynamic field and click submit this error appears: Undefined offset: 1
but it’s working with only one field.
i saw who someone who used isset but i don’t know how to use it in my case
Can anyone tell me that why such error occurs in php

Controller :

   public function store(Request $request)

    $data['invoice_no'] = $request->invoice_no;
    $data['last_invoice_no'] = $request->last_invoice_no;
    $data['devise'] = $request->devise;
    $data['customer_name'] = $request->customer_name;
    $data['customer_adress'] = $request->customer_adress;
    $data['invoice_no'] = $request->invoice_no;
    $data['invoice_date'] = $request->invoice_date;
    $data['company_adress'] = $request->company_adress;
    $data['company_name'] = $request->company_name;
    $data['company_phone'] = $request->company_phone;
    $data['poids_brut'] = $request->poids_brut;
    $data['poids_net'] = $request->poids_net;
    $data['livraison'] = $request->livraison;
    $data['incoterm'] = $request->incoterm;
    $data['payment_details'] = $request->payment_details;
    $data['sub_total'] = $request->sub_total;
    $data['shipping'] = $request->shipping;
    $data['total_due'] = $request->total_due;
    $data['created_by'] = (Auth::user()->name);

    $invoices = Invoices::create($data);

    $details_list = [];
    for ($i = 0; $i < count($request->categorie_id); $i++) {
        $details_list[$i]['categorie_id'] = $request->categorie_id[$i];
        $details_list[$i]['product_id'] = $request->product_id[$i];
        $details_list[$i]['size_id'] = $request->size_id[$i];
        $details_list[$i]['quantity'] = $request->quantity[$i];
        $details_list[$i]['unit_price'] = $request->unit_price[$i];
        $details_list[$i]['total_price'] = $request->total_price[$i];
        $details_list[$i]['created_by'] = (Auth::user()->name);

    $details = $invoices->details()->createMany($details_list);

    if ($details) {
        return redirect('/invoices')->with([
            'message' => __('la facture est créé avec succès'),
            'alert-type' => 'success'
    } else {
        return redirect()->back()->with([
            'message' => __('la creation de facture a échoué'),
            'alert-type' => 'danger'


Invoice Model :

class invoices extends Model
     protected $guarded=[];
    public function details()
        return $this->hasMany(invoice_products::class, 'invoice_id', 'id');

Invoice Products Model :

class invoice_products extends Model
    protected $guarded=[];

    public function invoice()
        return $this->belongsTo(Invoices::class, 'invoice_id', 'id');


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1


this error appears: Undefined offset: 1

You can find the explanation Here

Your Case:

for ($i = 0; $i < count($request->categorie_id); $i++) {
$details_list[$i][‘categorie_id’] = $request->categorie_id[$i];

Maybe (because we don’t know what you are passing from your form) your $request->categorie_id does not have a key/index 1. So, in your for loop, it looks for $request->categorie_id[1] and failing throws the error Undefined offset: 1, which means the array $request->categorie_id[] does not have the key/index/offset 1.


You can dd($request->all()); or dd($request->categorie_id); inside your store method to see what is inside to make sure it contains $request->categorie_id[] array with index 1

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x