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
24 changes: 23 additions & 1 deletion src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
Expand Down Expand Up @@ -1013,6 +1014,21 @@ public function foreignId($column)
]));
}

/**
* Create a new string (8-bit) column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
*/
public function foreignString($column)
{
return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
'type' => 'char',
'name' => $column,
'length' => Builder::$defaultStringLength,
]));
}

/**
* Create a foreign ID column for the given model.
*
Expand Down Expand Up @@ -1042,7 +1058,13 @@ public function foreignIdFor($model, $column = null)
->referencesModelColumn($model->getKeyName());
}

return $this->foreignUuid($column)
if (in_array(HasUuids::class, $modelTraits, true)) {
return $this->foreignUuid($column)
->table($model->getTable())
->referencesModelColumn($model->getKeyName());
}

return $this->foreignString($column)
->table($model->getTable())
->referencesModelColumn($model->getKeyName());
}
Expand Down
16 changes: 2 additions & 14 deletions tests/Database/Fixtures/Models/EloquentModelUsingUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

namespace Illuminate\Tests\Database\Fixtures\Models;

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

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

/**
* The "type" of the primary key ID.
*
* @var string
*/
protected $keyType = 'string';

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* Get the default foreign key name for the model.
*
Expand Down