Skip to content

Commit 1bf7f2b

Browse files
committed
Add unique like type migration
1 parent 442a5b6 commit 1bf7f2b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class LikeIsUniqueForUser extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
// user can like smth only once
17+
Schema::table('likes', function(Blueprint $blueprint) {
18+
$blueprint->unique(['user_id', 'likeable_id', 'likeable_type']);
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('likes', function(Blueprint $blueprint) {
30+
// need to drop foreign key before we can drop unique index
31+
$blueprint->dropForeign('likes_user_id_foreign');
32+
$blueprint->dropUnique(['user_id', 'likeable_id', 'likeable_type']);
33+
$blueprint
34+
->foreign('user_id')
35+
->references('id')
36+
->on('users')
37+
->onDelete('cascade');
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)