Search code examples
laravellaravel-5.3

Insert data to table Laravel?


I use the following code to inser data in table:

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

Model OrderProduct is:

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

Why I always get user_id is equal 0 in table order_product?

I tried also replace $user->id to real value :

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

But again get zero value in this field.

Type of user_id is:

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

Solution

  • Try adding it this way:

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

    By default Laravel adds the timestamp to the created_at and updated_at columns.

    Let me know if that works.