laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.52k stars 11.02k forks source link

Custom Pivot Models And Incrementing - Primary Key is missing when detaching relation #32255

Closed squiaios closed 4 years ago

squiaios commented 4 years ago

Description:

When using custom pivot class on model with incrementing to true, if I detach one relation, the id is nullable while deleting because method InteractsWithPivotTable->detachUsingCustomClass() create pivot object from model keys.

see spatie/laravel-activitylog#395 (comment) to get more information

driesvints commented 4 years ago

Please post full steps to reproduce.

squiaios commented 4 years ago

Migrations

Schema::create('artists', static function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('last_name');
    $table->string('first_name');
    $table->timestamps();
    $table->softDeletes();
});

Schema::create('nationalities', static function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('label');
    $table->timestamps();
});

Schema::create('artist_nationality', static function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('artist_id');
    $table->unsignedBigInteger('nationality_id');
});

Models

namespace App;

class Artist extends \Illuminate\Database\Eloquent\Model
{
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function nationalities()
    {
        return $this
            ->belongsToMany(Nationality::class)
            ->using(Pivots\ArtistNationality::class);
    }
}
namespace App;

class Nationality extends \Illuminate\Database\Eloquent\Model
{
    //
}

Pivot

namespace App\Pivots;

class ArtistNationality extends \Illuminate\Database\Eloquent\Relations\Pivot
{
    public $incrementing = true;
}

Insert data

$artist = \App\Artist::create(['last_name' => 'Guetta', 'first_name' => 'David']);
$nationalityFrench = \App\Nationality::create(['label' => 'french']);
$nationalityEnglish = \App\Nationality::create(['label' => 'english']);

$artist->nationalities()->sync([$nationalityFrench->id]);

Problem

When I sync with detach, if I want to use laravel model events, like deleted, the id parameters is not filled in detachUsingCustomClass

$artist->nationalities()->sync([$nationalityEnglish->id]);
driesvints commented 4 years ago

I'll leave this open for now and come back at a later time. Appreciating any help in the meantime.

petrenkorf commented 4 years ago

@driesvints can I take a look on this?

driesvints commented 4 years ago

@petrenkorf yes of course, any help welcome.

driesvints commented 4 years ago

I can't reproduce this sorry. Everything perfectly works when I try out your example. Feel free to provide even more thorough steps to reproduce and we'll have another look.

benounnas commented 1 year ago

i hope the documentation must explain more this section because i had the same issue too, i think the pivot incrementing should be true by default when a many to many relationship is created between the pivot model & other models