nova-batch-load icon indicating copy to clipboard operation
nova-batch-load copied to clipboard

Doesn't work using polymorphic relationships (Nova 3.8.2 / Laravel 7.23.2 / PHP 7.4.7)

Open Akak-mx opened this issue 4 years ago • 0 comments

Code example:

  • A coupon can have a code.
  • A coupon belongs to any of the next models: Reward, Experience or Ticket.
  • Any of those models can have many coupons.
  • A user can have many coupons.

The Nova resource:

App\Nova\Coupon.php
 
/**
 * Get the fields displayed by the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function fields(Request $request)
{
    return [
        Text::make('Código', 'code')
            ->rules('required', 'string', 'max:191'),

        MorphTo::make('Redeemable')
            ->types([
                Experience::class,
                Reward::class,
                Ticket::class,
            ])
            ->nullable(),

        BatchLoadField::make()
            ->accept('csv'),
    ];
}

The model:

 App\Models\Coupon

/**
 * Define an morph to relationship.
 *
 * @return \Illuminate\Database\Eloquent\Relations\MorphTo
 */
public function redeemable()
{
    return $this->morphTo();
}

A redeemable model:

App\Models\Reward

/**
 * Define a polymorphic one-to-many relationship.
 *
 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
 */
public function coupons(): MorphMany
{
    return $this->morphMany(Coupon::class, 'redeemable');
}

The migration:

CreateCouponsTable

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('coupons', function (Blueprint $table) {
        $table->increments('id');
        $table->nullableMorphs('redeemable');
        $table->timestamp('redeemed_at')->nullable();
        $table->unsignedBigInteger('user_id')->nullable();
        $table->string('code')->nullable();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

Am I doing something wrong?

Akak-mx avatar Sep 04 '20 15:09 Akak-mx