skip to Main Content

I was trying to store array data in the database, I didn’t get the error message and it doesn’t store it in the database. Here is my code in the blade file

<form method="post" action="{{route('CheckOut')}}" enctype="multipart/form-data">
    @csrf
    <div class="container">
        Name: <input type="text" name="costumer_name" value=""/>
        Address: <input type="text" name="costumer_address" value=""/>
        Contact Number <input type="text" name="costumer_number" value=""/>
    </div>
    @foreach ($carts as $cart)
        <div class="product">
            <div class="product-details">
                <input type="text" name="product[]" value="{{ $cart->product }}" />
            </div>
            <div class="product-quantity">
                <input type="number" value="{{ $cart->quantity }}" min="1" name="quantity[]">
            </div>
            <div>
                <a href="{{ route('remove', $cart->id) }}" class="btn btn-danger btn-sm">Remove</a>
            </div>
            <div>{{ $cart->subtotal }}</div>
        </div>
    @endforeach
    <input type="hidden" id="check_out_value" value="CheckOut" readonly/>
    <input type="submit" id= "checked_out_button" class="checkout" Value="Checkout">
</form>

I was trying to store the costumer_name, costumer_address, costumer_number together with the data that is in the loop which is the product[] and the quantity[] with id that is auto increment.

Here is my code in the controller

public function CheckOut(Request $request)
{
    $name = $request->costumer_name;
    $address = $request->costumer_adress;
    $number = $request->costumer_number;
    $products = $request->product;
    $quantity = $request->quantity;

    $data=[];
    foreach($products as $product) {
        $data[]=[
            'name'              => $name,
            'address'           => $address,
            'number'            => $number,
            'product'           => $product,
            'quantity'          => $quantity
        ];
    }
    Order::insert($data);
}

It gives me the error

Array to string conversion

Expected output in the database:

id name address number product quantity
1 John fili city 093-256-31 product1 100
2 John fili city 093-256-31 product2 200

2

Answers


  1. Looks to me you’re doing mass assignment on Order::insert(). Are all of the fields you are inserting in your Order model $fillable array? documentation

    Your Order model should contain something like this to even insert any data like you are doing it now:

    protected $fillable = ['name', 'address', 'number', 'product', 'quantity']
    

    Otherwise, you will get the current result and nothing will actually get stored in your orders table.

    Login or Signup to reply.
  2. The reason you’re getting

    Array to string conversion

    is because you’re inserting $quantity which is an array, but trying to put it into a single text field.

    You need to select the item from the quantity array which matches the product you’re inserting. If you use for loop to loop through products, then you can use the loop counter to easily select the right item from both the product and quantity arrays.

    For example:

    $data=[];
    for($i = 0; $i < count($products); $i++) {
        $data[]=[
            'name'              => $name,
            'address'           => $address,
            'number'            => $number,
            'product'           => $products[$i],
            'quantity'          => $quantity[$i]
        ];
    }
    Order::insert($data);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search