Skip to content

Commit

Permalink
Support withAttributes on many-to-many
Browse files Browse the repository at this point in the history
  • Loading branch information
tontonsb committed Dec 1, 2024
1 parent d1c8df4 commit 768de8d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ public function saveManyQuietly($models, array $pivotAttributes = [])
*/
public function create(array $attributes = [], array $joining = [], $touch = true)
{
$attributes = array_merge($this->getQuery()->pendingAttributes, $attributes);

$instance = $this->related->newInstance($attributes);

// Once we save the related model, we need to attach it to the base model via
Expand Down
120 changes: 120 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyWithAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentBelongsToManyWithAttributesTest extends TestCase
{
protected function setUp(): void
{
$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
$this->createSchema();
}

public function testCreatesWithAttributesAndPivotValues(): void
{
$post = ManyToManyWithAttributesPost::create();
$tag = $post->metaTags()->create(['name' => 'long article']);

$this->assertSame('long article', $tag->name);
$this->assertTrue($tag->visible);

$pivot = DB::table('with_attributes_pivot')->first();
$this->assertSame('meta', $pivot->type);
$this->assertSame($post->id, $pivot->post_id);
$this->assertSame($tag->id, $pivot->tag_id);
}

protected function createSchema()
{
$this->schema()->create('with_attributes_posts', function ($table) {
$table->increments('id');
$table->timestamps();
});

$this->schema()->create('with_attributes_tags', function ($table) {
$table->increments('id');
$table->string('name');
$table->boolean('visible')->nullable();
$table->timestamps();
});

$this->schema()->create('with_attributes_pivot', function ($table) {
$table->integer('post_id');
$table->integer('tag_id');
$table->string('type');
});
}

/**
* Tear down the database schema.
*
* @return void
*/
protected function tearDown(): void
{
$this->schema()->drop('with_attributes_posts');
$this->schema()->drop('with_attributes_tags');
$this->schema()->drop('with_attributes_pivot');
}

/**
* Get a database connection instance.
*
* @return \Illuminate\Database\Connection
*/
protected function connection($connection = 'default')
{
return Model::getConnectionResolver()->connection($connection);
}

/**
* Get a schema builder instance.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema($connection = 'default')
{
return $this->connection($connection)->getSchemaBuilder();
}
}

class ManyToManyWithAttributesPost extends Model
{
protected $guarded = [];
protected $table = 'with_attributes_posts';

public function tags(): BelongsToMany
{
return $this->belongsToMany(
ManyToManyWithAttributesTag::class,
'with_attributes_pivot',
'tag_id',
'post_id',
);
}

public function metaTags(): BelongsToMany
{
return $this->tags()
->withAttributes('visible', true)
->withPivotValue('type', 'meta');
}
}

class ManyToManyWithAttributesTag extends Model
{
protected $guarded = [];
protected $table = 'with_attributes_tags';
}

0 comments on commit 768de8d

Please sign in to comment.