【问题标题】:Column not found: 1054 Unknown column '0' in 'field list' in Laravel未找到列:1054 Laravel 中“字段列表”中的未知列“0”
【发布时间】:2017-04-12 11:11:11
【问题描述】:

我有一个函数可以将记录插入到 db。但是当我执行此函数时,我收到消息 SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'强>

Message

Database

我在控制器中的功能

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');
    }
}

我该如何解决这个问题?

【问题讨论】:

    标签: php frameworks laravel-5.2


    【解决方案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;
    }
    

    【讨论】:

    • 那么,如果我想将多条记录保存到数据库,我该如何使用保存方法执行?
    • 是的,@john 完全插入也可以,但你要阅读 laravel 5.2 的文档
    • 很高兴为您提供帮助 :)
    猜你喜欢
    • 2019-01-25
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2021-10-24
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多