0

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 
1
  • What happens if you use insert a record with pure SQL query? Commented Dec 18, 2016 at 0:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Happy to help.
0

controller.php

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.