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

devkon98's avatar

How to insert array values with createMany

Hello i have a form where the user selects client and product also insert an amount, the field of product and amount are dynamically added with a click of a button made with Javascript. This is the view code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
@section('content')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <div class="container" id="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">{{ __('Make Order') }}</div>

                    <div class="card-body" id="card-body">
                        <form method="POST" action="{{ route('orders.store') }}">
                            @csrf
                            <div class="row mb-3">
                                <label for="order_number" class="col-md-4 col-form-label text-md-end">{{ __('Order Number') }}</label>

                                <div class="col-md-6">
                                    <input id="order_number" type="orderNumber" class="form-control @error('order_number') is-invalid @enderror" name="order_number" value="{{ $randomNumber }}" required autocomplete="order_number">

                                    @error('order_number')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="row mb-3">
                                <label for="client" class="col-md-4 col-form-label text-md-end">{{ __('Client') }}</label>

                                <div class="col-md-6">
                                    <select name="client" id="client" type="text" class="form-control @error('client') is-invalid @enderror" required autocomplete="client">
                                        @foreach($clients as $client)
                                        <option value="{{$client->name}}">{{$client->name}}</option>
                                        @endforeach
                                    </select>


                                    @error('client')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="row mb-3">
                                <label for="products" class="col-md-4 col-form-label text-md-end">{{ __('Product') }}</label>

                                <div class="col-md-6">
                                    <select name="products[]" id="products" type="text" class="form-control @error('products') is-invalid @enderror" required autocomplete="products">
                                        @foreach($products as $product)
                                            <option value="{{$product->name}}">{{$product->name}}</option>
                                        @endforeach
                                    </select>

                                    @error('products')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="row mb-3">
                                <label for="amount" class="col-md-4 col-form-label text-md-end">{{ __('Amount') }}</label>

                                <div class="col-md-6">
                                    <input id="amount" type="text" class="form-control @error('amount') is-invalid @enderror" name="amount[]" required autocomplete="amount">

                                    @error('amount')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>


                            <div class="row mb-3" id="amm">

                            </div>

                            <div class="row mb-3">
                                <label for="description" class="col-md-4 col-form-label text-md-end">{{ __('Description') }}</label>

                                <div class="col-md-6" id="test">
                                    <input id="description" type="text" class="form-control @error('description') is-invalid @enderror" name="description" required autocomplete="description">

                                    @error('description')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="row">
                                <div class="col"></div>
                                <div class="col"><button type="submit" class="btn btn-primary">
                                        {{ __('Save') }}
                                    </button></div>
                                <div class="col"></div>
                                <div class="col"><button type="button" id="addbtn" onclick="add()" class="btn btn-primary"><span class="bi bi-plus"></span></button></div>
                            </div>
                        </form>
                    </div>
                </div>
                <p style="text-align: center"><a href="{{route('orders.index') }}">View all products</a></p>
            </div>
        </div>
    </div>
@endsection
<script>
   function add()
   {
       $('#amm').append(
               '<div class="row mb-4"><label for="products" class="col-md-4 col-form-label text-md-end">{{ __("Product") }}</label><div class="col-md-6"><select name="products[]" id="products" type="text" class="form-control @error("products") is-invalid @enderror" required autocomplete="products">@foreach($products as $product)<option value="{{$product->name}}">{{$product->name}}</option>@endforeach</select>@error("products")<span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>@enderror</div></div>   <div class="row mb-3"><label for="amount" class="col-md-4 col-form-label text-md-end">{{ __("Amount") }}</label><div class="col-md-6"><input id="amount" type="text" class="form-control @error("amount") is-invalid @enderror" name="amount[]" required autocomplete="amount">@error("amount")<span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>@enderror</div></div>'
       );
   }
</script>

This is my controller:

<?php

namespace App\Http\Controllers;

use App\Models\Client;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function index()
    {
        $orders = Order::all();
        return view('orders/index',compact('orders'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    public function create()
    {
        $randomNumber = bin2hex(openssl_random_pseudo_bytes(5));
        $clients = Client::all();
        $products = Product::all();
        return view('orders/create',compact('clients','products'))
            ->with('randomNumber',$randomNumber,(request()->input('page', 1) - 1) * 5);
    }

    public function store(Request $request)
    {
        $request->validate([
            'order_number' => 'required',
            'client' => 'required',
            'products' => 'required',
            'amount' => 'required',
            'description' => 'required',
        ]);
        $created_at = now();
        for($i = 0; $i < count($request->products); $i++)
        {
            $values[] = [
                'order_number' => $request->order_number,
                'client' => $request->client,
                'products' => $request->products[$i],
                'amount' => $request->amount[$i],
                'description' => $request->description,
                'created_at' => $created_at,
                'updated_at' => $created_at,
            ];
        }
        Order::insert($values);
        return redirect('/')->with('msg', 'Order Saved successfully!');
    }
}

I am trying to use createMany but i dont understand how it works and what other laravel developers told me is:

createMany() only works on relationships, what you should be using is insert()

0 likes
4 replies
Randy_Johnson's avatar

Why don't you just use <button type="submit">Submit</button> inside the form instead of using js since you're submitting the form anyway.

Also, your values is nested in the loop, so when you use Order::insert I don't think it can see it.

Also you could just try like this

$order = new Order;

for($i = 0; $i < count($request->products); $i++)
        {
                $order->order_number =  $request->order_number,
                $order->client =  $request->client,
                $order->products =  $request->products[$i],   // this could be a problem you may have to create another table and use relationships
               $order->amount=  $request->amount[$i], // same with above, create another table and use relationships
                $order->created_at=> $created_at,
        }

Looking at the code I just went over, it looks to me you will have to create a separate table for products. I would highly suggest you read into Eloquent and do some tutorials on it. It will save you a lot of trouble.

devkon98's avatar

@Randy_Johnson the Submit button is just to send the values, while the + button adds more html fields from JS the Order::insert works but i need to use createMany

devkon98's avatar

@Randy_Johnson Yes i am reading the documentation but i dont understand it all i have is a small piece of code https: //laravel.com/docs/9.x/eloquent-relationships#the-create-method is there any more to read that explains from scratch how do it?

Please or to participate in this conversation.