d_martiuk's avatar

Can't get model attributes which was created by default in DB

I have migration with setting default values

Schema::create('payments', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('type')->default(PaymentType::Base->value);
            $table->string('provider', 5)->default('w4p');
            $table->string('status')->default(PaymentStatus::NotPaid->value);
            $table->string('regularMode');
            $table->string('membership', 10)->default(PaymentMembership::Single->value);
            $table->string('clientFirstName')->nullable();
            $table->string('clientLastName')->nullable();
            $table->string('clientEmail')->nullable();
            $table->timestamp('orderDate')->useCurrent();
			..........
        });
    }

And after the entity is created, attributes that were created by default or cast return NULL. If you set dd after creation and set this created object, the attributes will not have the values that were created by default or were casted inside model

But If i get from created payment id and call Payment::find($payment->id), all the attributes are in place

0 likes
5 replies
d_martiuk's avatar

@JabatoForever Hi, shure

Controller

 public function store(PaymentRequest $request)
    {
        $payment = Payment::create($request->validated());

        $dump = Payment::find($payment->id);

        dd($payment->orderDate, $payment->status, $payment, $dump, $dump->orderDate);
    }

RequestForm

public function rules(): array
    {
        return [
            'provider' => ['sometimes', 'string', 'max:5'],
            'regularMode' => ['required',  new Enum(PaymentRegularMode::class)],
            'clientFirstName' => ['nullable', 'string', 'max:255'],
            'clientLastName' => ['nullable', 'string', 'max:255'],
            'clientEmail' => ['nullable', 'string', 'max:255'],
            'dateNext' => ['nullable', 'date_format:d.m.Y'],
           ........
        ];
    }

    protected function prepareForValidation()
    {
        $amount = $this->otherAmount ?? $this->amount;

        if (str_contains('monthly', $this->regularMode)){
            $productName = 'regular withdrawals';
            $dateNext = now()->copy()->addMonth()->format('d.m.Y');
        } else {
            $productName = 'single withdrawal';
            $dateNext = null;
        }

        $productPrice = $amount;

        $this->merge(compact('amount', 'dateNext', 'productName', 'productPrice'));
    }

Models/Payment.php

<?php

namespace App\Models;

use App\Enums\{PaymentMembership, PaymentRegularMode, PaymentStatus, PaymentType};
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    use HasFactory, HasUuids;

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'type',
        'provider',
        'status',
        'regularMode',
        'membership',

        'clientFirstName',
        'clientLastName',
        'clientEmail',

        # Signature
        'merchantSignature',

        # Dates
        'orderDate',
        'paymentDate',
        'dateNext',

        'amount',
        'currency',

        'productName',
        'productCount',
        'productPrice',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'type' => PaymentType::class,
        'status' => PaymentStatus::class,
        'regularMode' => PaymentRegularMode::class,
        'membership' => PaymentMembership::class,
        'orderDate' => 'date',
    ];
}
d_martiuk's avatar

I don't put here whole rules, cause it's unnecessary. Anyway payment created

Please or to participate in this conversation.