Skip to content
Open
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
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ Schema::create('posts', function(Blueprint $table) {
);
```

You can also manually specify which table and/or key to use as a reference by passing arguments to the `foreign` field like this: `logo_id:integer:foreign(images,key_name)`. You may have a logo image whose path is stored in an `images` table. This will give us:

```
$table->integer('logo_id');
$table->foreign('logo_id')->references('key_name')->on('images');
```

Don't pass the `key_name` to default to the `id` column.

Neato.

### Pivot Tables
Expand Down
16 changes: 16 additions & 0 deletions spec/Migrations/SchemaParserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ function it_parses_schema_fields_that_want_foreign_constraints()
['name' => 'user_id', 'type' => 'foreign', 'arguments' => [], 'options' => ['references' => "'id'", 'on' => "'users'"]]
]);
}

function it_parses_schema_fields_that_want_foreign_constraints_with_table()
{
$this->parse('logo_id:integer:foreign(images)')->shouldReturn([
['name' => 'logo_id', 'type' => 'integer', 'arguments' => [], 'options' => []],
['name' => 'logo_id', 'type' => 'foreign', 'arguments' => [], 'options' => ['references' => "'id'", 'on' => "'images'"]]
]);
}

function it_parses_schema_fields_that_want_foreign_constraints_with_table_and_primaryKey()
{
$this->parse('logo_id:integer:foreign(images,images_key)')->shouldReturn([
['name' => 'logo_id', 'type' => 'integer', 'arguments' => [], 'options' => []],
['name' => 'logo_id', 'type' => 'foreign', 'arguments' => [], 'options' => ['references' => "'images_key'", 'on' => "'images'"]]
]);
}
}
30 changes: 25 additions & 5 deletions src/Migrations/SchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ public function parse($schema)
$segments = $this->parseSegments($field);

if ($this->fieldNeedsForeignConstraint($segments)) {

// Get the foreign table and key
$foreignField = $segments['options']['foreign'];
$table = null;
$key = null;

if( $foreignField != 1 ){
$foreignOptions = explode(",", $foreignField);
list($table, $key) = count($foreignOptions) == 2 ? $foreignOptions : [$foreignField,null];
}

unset($segments['options']['foreign']);

// If the user wants a foreign constraint, then
// we'll first add the regular field.
$this->addField($segments);

// And then add another field for the constraint.
$this->addForeignConstraint($segments);
$this->addForeignConstraint($segments,$table,$key);

continue;
}
Expand Down Expand Up @@ -119,14 +130,23 @@ private function parseOptions($options)
/**
* Add a foreign constraint field to the schema.
*
* @param array $segments
* @param array $segments
* @param null|string $table
* @param null|string $key
*/
private function addForeignConstraint($segments)
private function addForeignConstraint($segments,$table = null, $key = null)
{
if( !isset($table) )
$table = $this->getTableNameFromForeignKey( $segments['name'] );

if( !isset($key) )
$key = 'id';

$string = sprintf(
"%s:foreign:references('id'):on('%s')",
"%s:foreign:references('%s'):on('%s')",
$segments['name'],
$this->getTableNameFromForeignKey($segments['name'])
$key,
$table
);

$this->addField($this->parseSegments($string));
Expand Down