Home > Mobile >  Laravel incomplete fields after data saving
Laravel incomplete fields after data saving

Time:08-03

I am not able to get all the fields after saving the data to the database.

orders table

 public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users');
            $table->string('payment_method')->nullable();
            $table->string('payment_status')->default('pending');
            $table->string('order_status')->default('payment_pending');
            $table->longText('address');
            $table->text('note')->nullable();
            $table->decimal('total', 8, 2)->default(0);
            $table->dateTime('payment_at')->nullable();
            $table->timestamps();
        });
    }

Order Modal

protected $fillable = [
        'user_id',
        'payment_method',
        'payment_status',
        'order_status',
        'address',
        'payment_at',
        'note',
        'total',
    ];

In my controller, I have the following functions:

public function createOrder($price)
    {
        $order = Order::create([
            'user_id' => 1,
            'address' => '123 Main St',
            'note' => 'dummy',
            'total' => $price
        ]);
    
    }

When I tried to save, I got the following output:

{
    "user_id": 1,
    "address": "123 Main St",
    "note": "dummy",
    "total": 548,
    "updated_at": "2022-08-02T12:55:47.000000Z",
    "created_at": "2022-08-02T12:55:47.000000Z",
    "id": 8
}

As you can see, I have got incomplete fields. How can I get all fields that can be null? screenshot enter image description here Thank you.

CodePudding user response:

In that case you can try:

public function createOrder($price)
    {
        $order = Order::create([
            'user_id' => 1,
            'address' => '123 Main St',
            'note' => 'dummy',
            'total' => $price
        ]);


    dd(Order::find($order->id));
    }

Should give you all the fields, including null

CodePudding user response:

You could do something like this using null coalescing to enter fields which are null:

public function createOrder($price)
    {
        $order = Order::create(
          [
        'user_id' => 1,
        'payment_method' => $payment_method ?? null,
        'payment_status' => $payment_status ?? null,
        'order_status' => $order_status ?? null,
        'address' => '123 Main St' ?? null,
        'payment_at' => $payment_at ?? null,
        'note' => $note ?? null,
        'total' => $price ?? null,
          ]
    );
    
    }

But its highly recommended you validate the fields before you call the create method. Validation will also give you the null values.

CodePudding user response:

Add the column you want to be return on the create

public function createOrder($price)
    {
        $order = Order::create([
            'user_id' => 1,
            'payment_method' => null,
            'payment_status' => null,
            'address' => '123 Main St',
            'note' => 'dummy',
            'total' => $price
        ]);
    
    }
  • Related