1

I have store method in my controller and it returns this errors:

Errors

Console Preview tab

exception: "ErrorException"
file: "C:\........\vendor\laravel\framework\src\Illuminate\Support\Str.php"
line: 419
message: "Array to string conversion"

Console Response tab

   {
        "file": "C:\\........\\app\\Http\\Controllers\\Api\\Front\\CartController.php",
        "line": 380,
        "function": "save",
        "class": "Illuminate\\Database\\Eloquent\\Model",
        "type": "->"
    },

Code

public function checkout(Request $request)
{
    $user = auth('api')->user();
    $cartItems = CartStorage::where('user_id', $user->id)->get();
    $address = AddressUser::where('id', $request->input('address'))->first();

    foreach($cartItems as $item) {
        $cartData = $item->cart_data;
        // add to orders table
        try {
            $order = new Order();
            $order->ordernu = 'Si-'.mt_rand(1000000000, 9999999999);
            $order->user_id = $user->id;
            $order->order_data = $cartData;
            $order->quantity = $cartData['quantity'];
            $order->price = $request->input('totalPrice');
            $order->courier = $request->input('courier');
            $order->courier_service = $request->input('courierService');
            $order->shippingcode = $request->input('shippingcode');
            $order->shipping_price = $request->input('shippingPrice');
            $order->address = $address->address;
            $order->kecamatan = $address->kecamatan;
            $order->kelurahan = $address->kelurahan;
            $order->kota = $address->kota;
            $order->provinsi = $address->provinsi;
            $order->postalcode = $address->postalcode;
            $order->weight = $request->input('weight');
            $order->phone = $request->input('phone');
            $order->buyer_name = $user->name;
            $order->buyer_email = $user->email;
            $order->note = $request->input('note');
            $order->received = false;
            $order->save();

            // reduce stock of each product
            foreach ($cartItems as $item) {
                $cartData = $item->cart_data;
                $product = Product::find($cartData['productId']);
                $product->decrement('qty', $cartData['quantity']);
            }

        } catch (Exception $e) {
            return response($e->getMessage(), 400);
        }
    }
}

Note: I've tested dd every single data that I placed in order column and all are getting true values.

Any idea?

3 Answers 3

3

I think the problem is here:

$order->order_data = $cartData;

$cartData is an array and cannot be saved into database directly.

That's why you got Array to string conversion error.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly, I've just solved it with json_encode($cartData); thank you
2

I was seeking for the same solution and this worked for me. Assuming your order_data column on the Order model is of type json. Define the casts property in your Order model with order_data as an array. You won't need json_encode if you use it this way as laravel will convert the array into json for you.

class Order extends Model
{
    protected $casts = [
        'order_data' => 'array'
    ];
}

Comments

0

this because you try to store array as a string into the database, to store array into the database you have either :

  1. $data_to_store=json_encode($array) then when callback $array=json_decode($db_data)
  2. $data_to_store=serialize($array) then when callback $array=unserialize($db_data)
  3. $data_to_store= implode('!!', $array) then when callback $array = explode('!!', $db_data)

check also this amazing answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.