将数据插入表Laravel?

时间:2016-12-17 21:39:15

标签: laravel laravel-5.3

我将以下代码用于表中的inser数据:

$order = new OrderProduct();
        $order->save([
            "created_at" => Carbon::now(),
            "user_id" => $user->id,
            "note" => $request->note
        ]);

模型OrderProduct是:

class OrderProduct extends Model
{

    public $timestamps = true;

    protected $table = "class OrderProduct extends Model

{

public $timestamps = true;

protected $table = "order_product";

protected $fillable = [
    'order_id', 'status', 'user_id', 'note'
];";

    protected $fillable = [
        'order_id', 'status', 'user_id', 'note'
    ];
}

为什么我总是user_id在表order_product中等于0?

我尝试将$user->id替换为实际值:

$order->save([
                "created_at" => Carbon::now(),
                "user_id" => 2,
                "note" => $request->note
            ]);

但是在这个领域再次获得零值。

user_id的类型是:

| user_id    | int(11)      | NO   |     | NULL 

2 个答案:

答案 0 :(得分:1)

尝试以这种方式添加:

$order = new OrderProduct();
$order->user_id = 2;
$order->note = $noteHere;
$order->save():

默认情况下,Laravel会将时间戳添加到created_at和updated_at列。

如果有效,请告诉我。

答案 1 :(得分:0)

controller.php

      $name  = $request->name;
     $email  = $request->email;
        $data = array(
            "name" =>  $name,
            "email"=>  $email
            );

        $q = DB::table('users')->insert($data);