Closed squiaios closed 4 years ago
Please post full steps to reproduce.
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');
});
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
{
//
}
namespace App\Pivots;
class ArtistNationality extends \Illuminate\Database\Eloquent\Relations\Pivot
{
public $incrementing = true;
}
$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]);
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]);
I'll leave this open for now and come back at a later time. Appreciating any help in the meantime.
@driesvints can I take a look on this?
@petrenkorf yes of course, any help welcome.
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.
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
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