Skip to content

Commit

Permalink
[10.x] Use foreignUlid if model uses HasUlids trait when call foreign…
Browse files Browse the repository at this point in the history
…IdFor (laravel#46876)

* fix: use foreignUlid if model uses HasUlids

* test: add assert for MySql

* Update Blueprint.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
lotarbo and taylorotwell authored Apr 25, 2023
1 parent b5bef6b commit e2a65b3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BadMethodCallException;
use Closure;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Database\SQLiteConnection;
Expand Down Expand Up @@ -938,9 +939,19 @@ public function foreignIdFor($model, $column = null)
$model = new $model;
}

return $model->getKeyType() === 'int' && $model->getIncrementing()
? $this->foreignId($column ?: $model->getForeignKey())
: $this->foreignUuid($column ?: $model->getForeignKey());
$column = $column ?: $model->getForeignKey();

if ($model->getKeyType() === 'int' && $model->getIncrementing()) {
return $this->foreignId($column);
}

$modelTraits = class_uses_recursive($model);

if (in_array(HasUlids::class, $modelTraits, true)) {
return $this->foreignUlid($column);
}

return $this->foreignUuid($column);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,29 @@ public function testGenerateRelationshipColumnWithUuidModel()
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testGenerateRelationshipColumnWithUlidModel()
{
require_once __DIR__.'/stubs/EloquentModelUlidStub.php';

$base = new Blueprint('posts', function (Blueprint $table) {
$table->foreignIdFor('EloquentModelUlidStub');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table "posts" add column "eloquent_model_ulid_stub_id" char(26) not null',
], $blueprint->toSql($connection, new PostgresGrammar));

$blueprint = clone $base;

$this->assertEquals([
'alter table `posts` add `eloquent_model_ulid_stub_id` char(26) not null',
], $blueprint->toSql($connection, new MySqlGrammar()));
}

public function testDropRelationshipColumnWithIncrementalModel()
{
$base = new Blueprint('posts', function ($table) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/stubs/EloquentModelUlidStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;

class EloquentModelUlidStub extends Model
{
use HasUlids;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'model';
}

0 comments on commit e2a65b3

Please sign in to comment.