Skip to content

Commit e2a65b3

Browse files
[10.x] Use foreignUlid if model uses HasUlids trait when call foreignIdFor (#46876)
* fix: use foreignUlid if model uses HasUlids * test: add assert for MySql * Update Blueprint.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b5bef6b commit e2a65b3

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BadMethodCallException;
66
use Closure;
77
use Illuminate\Database\Connection;
8+
use Illuminate\Database\Eloquent\Concerns\HasUlids;
89
use Illuminate\Database\Query\Expression;
910
use Illuminate\Database\Schema\Grammars\Grammar;
1011
use Illuminate\Database\SQLiteConnection;
@@ -938,9 +939,19 @@ public function foreignIdFor($model, $column = null)
938939
$model = new $model;
939940
}
940941

941-
return $model->getKeyType() === 'int' && $model->getIncrementing()
942-
? $this->foreignId($column ?: $model->getForeignKey())
943-
: $this->foreignUuid($column ?: $model->getForeignKey());
942+
$column = $column ?: $model->getForeignKey();
943+
944+
if ($model->getKeyType() === 'int' && $model->getIncrementing()) {
945+
return $this->foreignId($column);
946+
}
947+
948+
$modelTraits = class_uses_recursive($model);
949+
950+
if (in_array(HasUlids::class, $modelTraits, true)) {
951+
return $this->foreignUlid($column);
952+
}
953+
954+
return $this->foreignUuid($column);
944955
}
945956

946957
/**

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,29 @@ public function testGenerateRelationshipColumnWithUuidModel()
372372
], $blueprint->toSql($connection, new MySqlGrammar));
373373
}
374374

375+
public function testGenerateRelationshipColumnWithUlidModel()
376+
{
377+
require_once __DIR__.'/stubs/EloquentModelUlidStub.php';
378+
379+
$base = new Blueprint('posts', function (Blueprint $table) {
380+
$table->foreignIdFor('EloquentModelUlidStub');
381+
});
382+
383+
$connection = m::mock(Connection::class);
384+
385+
$blueprint = clone $base;
386+
387+
$this->assertEquals([
388+
'alter table "posts" add column "eloquent_model_ulid_stub_id" char(26) not null',
389+
], $blueprint->toSql($connection, new PostgresGrammar));
390+
391+
$blueprint = clone $base;
392+
393+
$this->assertEquals([
394+
'alter table `posts` add `eloquent_model_ulid_stub_id` char(26) not null',
395+
], $blueprint->toSql($connection, new MySqlGrammar()));
396+
}
397+
375398
public function testDropRelationshipColumnWithIncrementalModel()
376399
{
377400
$base = new Blueprint('posts', function ($table) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Illuminate\Database\Eloquent\Concerns\HasUlids;
4+
use Illuminate\Database\Eloquent\Model;
5+
6+
class EloquentModelUlidStub extends Model
7+
{
8+
use HasUlids;
9+
/**
10+
* The table associated with the model.
11+
*
12+
* @var string
13+
*/
14+
protected $table = 'model';
15+
}

0 commit comments

Comments
 (0)