New to Laravel
What is the difference between foreignId() and unsignedBigInteger() while linking tables
$table->unsignedBigInteger('user_id'); $table->foreignId('user_id');
I’ve tried both and they all worked.
According to the documentation it says:
The
foreignId
method is an alias forunsignedBigInteger
but what does alias mean? Does it mean they are the same?
PS: I didn’t use the code in the documentation but only
$table->unsignedBigInteger('user_id');
and/or
$table->foreignId('user_id');
Advertisement
Answer
If you have a look in Blueprint.php you’ll see both methods :
/** * Create a new unsigned big integer (8-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @return IlluminateDatabaseSchemaColumnDefinition */ public function unsignedBigInteger($column, $autoIncrement = false) { return $this->bigInteger($column, $autoIncrement, true); } /** * Create a new unsigned big integer (8-byte) column on the table. * * @param string $column * @return IlluminateDatabaseSchemaForeignIdColumnDefinition */ public function foreignId($column) { $this->columns[] = $column = new ForeignIdColumnDefinition($this, [ 'type' => 'bigInteger', 'name' => $column, 'autoIncrement' => false, 'unsigned' => true, ]); return $column; }
So, by default it uses “bigInteger” column’s type with “unsigned” set to true. In the end, they are the same.
The only difference would be that with “unsignedBigInteger” you can control if $autoIncrement is set to true or false, not with foreignId