Laravel saving polymorphic model doesn't have a default value

333 Views Asked by At

I'm working on a Laravel 9 project. I'm new to Polymorphic relationships and believe I need to define one.

In my application, I have a parent model called Application that has a single ApplicationGBPayday model, but it could have other models in the future for different products/countries.

I've defined my Application model schema to contain a modelable morph fieldset, and in my controller where I create an Application I then need to create and link the ApplicationGBPayday, but I'm getting an error right now:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364 Field 'modelable_type' doesn't have a default value

My controller I'm doing:

// create application
$application = Application::create([
    'user_id' => $affiliate->user_id,
    'company_id' => $affiliate->company_id,
    'country_id' => $affiliate->affiliate_product->country->id,
    'product_id' => $affiliate->affiliate_product->product->id,
    'serve_method_id' => $affiliate->affiliate_product->serve_method->id,
    'application_form_id' => $affiliate->affiliate_product->application_form->id,
    'pingtree_group_id' => $affiliate->affiliate_product->pingtree_group->id,
    'affiliate_id' => $affiliate->id,
    'thread_uuid' => $threadUUID,
    'status' => 'pending',
    'submitted_at' => $submittedAt
]);

$payday = ApplicationGBPayday::create($request->all());

And here's my model for Application:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Casts\Json;

class Application extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'applications';

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'submitted_at' => 'datetime',
    ];

    /**
     * Get the parent modelable model.
     */
    public function modelable()
    {
        return $this->morphTo();
    }
}

And ApplicationGBPayday:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Casts\Json;

class ApplicationGBPayday extends Model
{
    use HasFactory;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'application_gb_paydays';

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'buyer_list' => Json::class,
        'buyer_list_pending' => Json::class,
        'buyer_list_accepted' => Json::class,
        'buyer_list_declined' => Json::class,
        'buyer_list_invalid' => Json::class,
        'buyer_list_skipped' => Json::class,
        'other_data_json' => Json::class,
        'response_json' => Json::class,
    ];

    /**
     * Get the model's application
     */
    public function application()
    {
        return $this->morphOne(Application::class, 'modelable');
    }
}

I'm struggling to figure out how to save the morph fields since they can't be null here, yet I need the application created first and then the payday? What am I missing?

UPDATE

I've tried:

// create application
$application = Application::create([
    'user_id' => $affiliate->user_id,
    'company_id' => $affiliate->company_id,
    'country_id' => $affiliate->affiliate_product->country->id,
    'product_id' => $affiliate->affiliate_product->product->id,
    'serve_method_id' => $affiliate->affiliate_product->serve_method->id,
    'application_form_id' => $affiliate->affiliate_product->application_form->id,
    'pingtree_group_id' => $affiliate->affiliate_product->pingtree_group->id,
    'affiliate_id' => $affiliate->id,
    'thread_uuid' => $threadUUID,
    'status' => 'pending',
    'submitted_at' => $submittedAt
]);

$payday = new ApplicationGBPayday($request->all());
$application->modelable()->save($payday);

Which gives me:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364 Field 'modelable_type' doesn't have a default value (SQL: insert into applications

0

There are 0 best solutions below