dongyong1897 2017-04-12 11:11
浏览 70
已采纳

未找到列:1054 Laravel中“字段列表”中的未知列“0”

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'

Message

Database

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?

  • 写回答

2条回答 默认 最新

  • doubingguan3425 2017-04-12 11:24
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?