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()