我有一个函数,我在执行此函数时将recort插入db.But,我收到消息 SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'0'
我在控制器中的功能
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);
}
在模型中
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');
}
}
我该如何解决这个问题?
答案 0 :(得分:1)
使用保存功能而不是插入
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;
}
答案 1 :(得分:0)
$orderArr=array($order);
所以你的$ orderArr会是这样的:
$orderArr=array:2 [▼
2 => "2"
4 => "4"
])
我有这个问题,因为我在我的视图中使用了这样的东西
<input name="media[{{$media->id}}]"
之后我将其改为
<input name="{{$media->id}}]"
我的问题消失了