Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public function firstOrCreate(array $attributes, array $joining = [], $touch = t
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
{
if (is_null($instance = $this->where($attributes)->first())) {
return $this->create($values, $joining, $touch);
return $this->create(array_merge($attributes, $values), $joining, $touch);
}

$instance->fill($values);
Expand Down
138 changes: 138 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Illuminate\Tests\Database;

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

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

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->bootEloquent();
$db->setAsGlobal();

$this->createSchema();
}

/**
* Setup the database schema.
*
* @return void
*/
public function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->id('id');
});

$this->schema()->create('articles', function ($table) {
$table->id('id');

$table->string('title');
$table->string('description');
});

$this->schema()->create('article_user', function ($table) {
$table->foreignId('article_id')->references('id')->on('articles');
$table->foreignId('user_id')->references('id')->on('users');
});
}

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

public function testCreateWithDesiredAttributesUsingUpdateOrCreate()
{
/** @var FakeUpdateOrCreateUser $user */
$user = FakeUpdateOrCreateUser::create();

$user->articles()->updateOrCreate(
['title' => 'Fixing UpdateOrCreate'],
['description' => 'Fixed']
);

$article = $user->articles()->first();

$this->assertSame($article->description, 'Fixed');
}

public function testUpdateWithDesiredAttributesUsingUpdateOrCreate()
{
/** @var FakeUpdateOrCreateUser $user */
$user = FakeUpdateOrCreateUser::create();
$user->articles()->create([
'title' => $title = 'Fixing UpdateOrCreate',
'description' => 'Not fixed',
]);

$user->articles()->updateOrCreate(
['title' => $title],
['description' => 'Fixed']
);

$article = $user->articles()->first();

$this->assertSame($article->description, 'Fixed');
}

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

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

class FakeUpdateOrCreateUser extends Model
{
protected $table = 'users';

protected $fillable = ['email'];

public $timestamps = false;

public function articles()
{
return $this->belongsToMany(FakeUpdateOrCreateArticle::class, 'article_user', 'user_id', 'article_id');
}
}

class FakeUpdateOrCreateArticle extends Model
{
protected $table = 'articles';

public $timestamps = false;

protected $fillable = ['title', 'description'];
}