Forráskód Böngészése

migrations and models

Carello Qwerty 6 éve
szülő
commit
51c43ca096

+ 12 - 0
app/Group.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Group extends Model
+{
+    public function users(){
+        return $this->belongsToMany(User::class);
+    }
+}

+ 13 - 0
app/Location.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Location extends Model
+{
+    public function users(){
+        return $this->belongsTo(User::class);
+    }
+
+}

+ 12 - 0
app/Role.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Role extends Model
+{
+    public function users (){
+        return $this->hasMany(User::class);
+    }
+}

+ 13 - 1
app/User.php

@@ -16,7 +16,7 @@ class User extends Authenticatable
      * @var array
      */
     protected $fillable = [
-        'name', 'email', 'password',
+        'name', 'email', 'telegram_id', 'google_id', 'role_id', 'password',
     ];
 
     /**
@@ -36,4 +36,16 @@ class User extends Authenticatable
     protected $casts = [
         'email_verified_at' => 'datetime',
     ];
+
+    public function roles(){
+        return $this->belongsTo(Role::class);
+    }
+
+    public function locations(){
+        return $this->hasMany(Location::class);
+    }
+
+    public function groups(){
+        return $this->belongsToMany(Group::class);
+    }
 }

+ 3 - 2
database/migrations/2014_10_12_000000_create_users_table.php

@@ -17,9 +17,10 @@ class CreateUsersTable extends Migration
             $table->increments('id');
             $table->string('name');
             $table->string('email')->unique();
-            $table->integer('telegram_id');
-            $table->timestamp('email_verified_at')->nullable();
+            $table->integer('telegram_id')->nullable();
+            $table->integer('google_id')->nullable();
             $table->string('password');
+            $table->timestamp('email_verified_at')->nullable();
             $table->rememberToken();
             $table->timestamps();
         });

+ 38 - 0
database/migrations/2019_02_27_102228_create_locations_table.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateLocationsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('locations', function (Blueprint $table) {
+            $table->increments('id');
+            $table->float('lat');
+            $table->float('lng');
+            $table->unsignedInteger('user_id');
+            $table->foreign('user_id')
+                ->references('id')
+                ->on('users')
+                ->onDelete('cascade');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('locations');
+    }
+}

+ 32 - 0
database/migrations/2019_02_27_102258_create_groups_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateGroupsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('groups', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('name');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('groups');
+    }
+}

+ 32 - 0
database/migrations/2019_02_27_102330_create_roles_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateRolesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('roles', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('name');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('roles');
+    }
+}

+ 43 - 0
database/migrations/2019_02_27_102408_groups_users.php

@@ -0,0 +1,43 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class GroupsUsers extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('groups_users', function (Blueprint $table) {
+            $table->unsignedInteger('user_id');
+            $table->foreign('user_id', 'fk_users')
+                ->references('id')
+                ->on('users')
+                ->onDelete('cascade');
+            $table->unsignedInteger('group_id');
+            $table->foreign('group_id', 'fk_groups')
+                ->references('id')
+                ->on('groups')
+                ->onDelete('cascade');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('groups_users', function (Blueprint $table) {
+            $table->dropForeign('fk_users');
+            $table->dropForeign('fk_groups');
+        });
+        Schema::dropIfExists('groups_users');
+    }
+}

+ 36 - 0
database/migrations/2019_02_27_104135_add_role_id_to_users_table.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddRoleIdToUsersTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->unsignedInteger('role_id')->after('google_id');
+            $table->foreign('role_id', 'fk_roles')
+                ->references('id')
+                ->on('roles');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropForeign('fk_roles');
+            $table->dropColumn('role_id');
+        });
+    }
+}