Skip to content

Commit

Permalink
add helper for on delete cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 6, 2019
1 parent 06307a1 commit ec966c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Illuminate/Database/Schema/Blueprint.php
Expand Up @@ -5,8 +5,9 @@
use BadMethodCallException;
use Closure;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\Schema\ForeignKeyDefinition;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Support\Fluent;
use Illuminate\Support\Traits\Macroable;

Expand Down Expand Up @@ -520,11 +521,17 @@ public function spatialIndex($columns, $name = null)
*
* @param string|array $columns
* @param string|null $name
* @return \Illuminate\Support\Fluent|\Illuminate\Database\Schema\ForeignKeyDefinition
* @return \Illuminate\Database\Schema\ForeignKeyDefinition
*/
public function foreign($columns, $name = null)
{
return $this->indexCommand('foreign', $columns, $name);
$command = new ForeignKeyDefinition(
$this->indexCommand('foreign', $columns, $name)->getAttributes()
);

$this->commands[count($this->commands) - 1] = $command;

return $command;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Illuminate/Database/Schema/ForeignKeyDefinition.php
Expand Up @@ -14,5 +14,13 @@
*/
class ForeignKeyDefinition extends Fluent
{
//
/**
* Indicate that deletes should cascade.
*
* @return $this
*/
public function cascadeOnDelete()
{
return $this->onDelete('cascade');
}
}
8 changes: 8 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Expand Up @@ -371,6 +371,14 @@ public function testAddingForeignKey()

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`)', $statements[0]);


$blueprint = new Blueprint('users');
$blueprint->foreign('foo_id')->references('id')->on('orders')->cascadeOnDelete();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on delete cascade', $statements[0]);
}

public function testAddingIncrementingID()
Expand Down

0 comments on commit ec966c2

Please sign in to comment.