Reputation: 7128
I have store method in my controller and it returns this 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": "->"
},
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 inorder
column and all are getting true values.
Any idea?
Upvotes: 1
Views: 13030
Reputation: 256
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'
];
}
Upvotes: 2
Reputation: 983
this because you try to store array as a string into the database, to store array into the database you have either :
$data_to_store=json_encode($array)
then when callback $array=json_decode($db_data)
$data_to_store=serialize($array)
then when callback $array=unserialize($db_data)
$data_to_store= implode('!!', $array)
then when callback $array = explode('!!', $db_data)
check also this amazing answer
Upvotes: 0
Reputation: 8459
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.
Upvotes: 3