Insert data to table Laravel?

1k views Asked by At

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 
2

There are 2 answers

1
Scott On BEST ANSWER

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.

0
darshan On

controller.php

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

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