Completed
Push — master ( 43d6b6...7fd3dc )
by Sherif
12:33
created

Groups::up()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
use Illuminate\Database\Schema\Blueprint;
4
use Illuminate\Database\Migrations\Migration;
5
6
class Groups extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
	/**
9
	 * Run the migrations.
10
	 *
11
	 * @return void
12
	 */
13
	public function up()
14
	{
15
		Schema::create('groups', function (Blueprint $table) {
16
            $table->increments('id');
17
            $table->string('name',100)->unique();
18
            $table->softDeletes();
19
            $table->timestamps();
20
        });
21
        
22
        Schema::create('users_groups', function (Blueprint $table) {
23
            $table->increments('id');
24
            $table->integer('user_id');
25
            $table->integer('group_id');
26
            $table->softDeletes();
27
            $table->timestamps();
28
29
	        $table->index(['user_id']);
30
        });
31
        
32
		/**
33
		 * Create Default groups.
34
		 */
35
		\DB::table('groups')->insert(
36
			[
37
				[
38
					'name'       => 'Admin',
39
					'created_at' => \DB::raw('NOW()'),
40
					'updated_at' => \DB::raw('NOW()')
41
				]
42
			]
43
		);
44
        
45
		/**
46
		 * Assign default users to admin groups.
47
		 */
48
		$adminGroupId = \DB::table('groups')->where('name', 'admin')->select('id')->first()->id;
49
		$adminUserId  = \DB::table('users')->where('email', '[email protected]')->select('id')->first()->id;
50
		\DB::table('users_groups')->insert(
51
			[
52
			'user_id'    => $adminUserId,
53
			'group_id'   => $adminGroupId,
54
			'created_at' => \DB::raw('NOW()'),
55
			'updated_at' => \DB::raw('NOW()')
56
			]
57
        );
58
	}
59
60
	/**
61
	 * Reverse the migrations.
62
	 *
63
	 * @return void
64
	 */
65
	public function down()
66
	{
67
		Schema::dropIfExists('groups');
68
		Schema::dropIfExists('users_groups');
69
	}
70
}