I have a function where i insert recort to db.But when i execute this function, i get the message SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'
My function in controller
public function postCheckout(Request $request){
if(!Session::has('cart')){
return view('cart.shopping_cart');
}
$data = $request->all();
$oldCart=Session::get('cart');
$cart=new Cart($oldCart);
$order=new Order();
$order->order_code="Order00006";
$order->user_id=Auth::user()?Auth::user()->id:1;
$order->full_name=$data['full_name'];
$order->phone=$data['phone'];
$order->address=$data['address'];
$order->recieve_address=$data['recieve_address'];
$order->total_product=$cart->totalQuantity;
$order->total_price=$cart->totalPrice;
$orderArr=array($order);
Order::insert($orderArr);
}
In model
class Order extends Model
{
protected $table="orders";
protected $fillable = [
'order_code','user_id','full_name','phone', 'address', 'recieve_address', 'total_product','total_price' ,
];
public function user(){
return $this->belongsTo('App\User');
}
}
How can i fix this?
Use Save function instead of insert
public function postCheckout(Request $request){
if(!Session::has('cart')){
return view('cart.shopping_cart');
}
$data = $request->all();
$oldCart=Session::get('cart');
$order=new Order();
$order->order_code="Order00006";
$order->user_id=Auth::user()?Auth::user()->id:1;
$order->full_name=$data['full_name'];
$order->phone=$data['phone'];
$order->address=$data['address'];
$order->recieve_address=$data['recieve_address'];
$order->total_product=$oldCart->totalQuantity;
$order->total_price=$oldCart->totalPrice;
$order->save();
echo "Data saved"; die;
}
hello guys i think it's because you are using 2d array
$orderArr=array($order);
so your $orderArr is will be some thing like this:
$orderArr=array:2 [▼
2 => "2"
4 => "4"
])
i had this problem because i was using some thing like this in my view
<input name="media[{{$media->id}}]"
after i changed it to
<input name="{{$media->id}}]"
my problem vanished