yakoffka.ru
    грабли, костыли и велосипеды php, css, html, js и прочего

    Создание ролей и разрешений в laravel 5.7

    laravel Логи выполнения тестового задания по laravel №2

    upd: НЕ ДЕЛАЙТЕ ТАК НИКОГДА!!!

    Капустин Яков

    оглавление

    Установка приложения:

    bash:
    vagrant@homestead:~/projects$ composer create-project --prefer-dist laravel/laravel task2 Installing laravel/laravel (v5.7.19) - Installing laravel/laravel (v5.7.19): Loading from cache Created project in task2 > @php -r "file_exists('.env') || copy('.env.example', '.env');" Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 86 installs, 0 updates, 0 removals - Installing symfony/polyfill-ctype (v1.10.0): Loading from cache - Installing vlucas/phpdotenv (v2.6.1): Loading from cache - Installing symfony/css-selector (v4.2.3): Loading from cache - Installing tijsverkoyen/css-to-inline-styles (2.2.1): Loading from cache - Installing symfony/polyfill-php72 (v1.10.0): Loading from cache - Installing symfony/polyfill-mbstring (v1.10.0): Loading from cache - Installing symfony/var-dumper (v4.2.3): Loading from cache - Installing symfony/routing (v4.2.3): Loading from cache - Installing symfony/process (v4.2.3): Loading from cache - Installing symfony/http-foundation (v4.2.3): Loading from cache - Installing symfony/contracts (v1.0.2): Loading from cache - Installing symfony/event-dispatcher (v4.2.3): Loading from cache - Installing psr/log (1.1.0): Loading from cache - Installing symfony/debug (v4.2.3): Loading from cache - Installing symfony/http-kernel (v4.2.3): Loading from cache - Installing symfony/finder (v4.2.3): Loading from cache - Installing symfony/console (v4.2.3): Loading from cache - Installing doctrine/lexer (v1.0.1): Loading from cache - Installing egulias/email-validator (2.1.7): Loading from cache - Installing swiftmailer/swiftmailer (v6.1.3): Loading from cache - Installing paragonie/random_compat (v9.99.99): Loading from cache - Installing ramsey/uuid (3.8.0): Loading from cache - Installing psr/simple-cache (1.0.1): Loading from cache - Installing psr/container (1.0.0): Loading from cache - Installing opis/closure (3.1.5): Loading from cache - Installing symfony/translation (v4.2.3): Loading from cache - Installing nesbot/carbon (1.36.2): Loading from cache - Installing monolog/monolog (1.24.0): Loading from cache - Installing league/flysystem (1.0.50): Loading from cache - Installing erusev/parsedown (1.7.1): Loading from cache - Installing dragonmantank/cron-expression (v2.2.0): Loading from cache - Installing doctrine/inflector (v1.3.0): Loading from cache - Installing ralouphie/getallheaders (2.0.5): Loading from cache - Installing psr/http-message (1.0.1): Loading from cache - Installing guzzlehttp/psr7 (1.5.2): Loading from cache - Installing guzzlehttp/promises (v1.3.1): Loading from cache - Installing guzzlehttp/guzzle (6.3.3): Loading from cache - Installing laravel/slack-notification-channel (v1.0.3): Loading from cache - Installing laravel/framework (v5.7.25): Loading from cache - Installing lcobucci/jwt (3.2.5): Loading from cache - Installing php-http/promise (v1.0.0): Loading from cache - Installing php-http/httplug (v1.1.0): Loading from cache - Installing php-http/guzzle6-adapter (v1.1.1): Loading from cache - Installing zendframework/zend-diactoros (1.8.6): Loading from cache - Installing nexmo/client (1.6.2): Loading from cache - Installing laravel/nexmo-notification-channel (v1.0.1): Loading from cache - Installing fideloper/proxy (4.1.0): Loading from cache - Installing nikic/php-parser (v4.2.0): Loading from cache - Installing jakub-onderka/php-console-color (v0.2): Loading from cache - Installing jakub-onderka/php-console-highlighter (v0.4): Loading from cache - Installing dnoegel/php-xdg-base-dir (0.1): Loading from cache - Installing psy/psysh (v0.9.9): Loading from cache - Installing laravel/tinker (v1.0.8): Loading from cache - Installing beyondcode/laravel-dump-server (1.2.2): Loading from cache - Installing fzaninotto/faker (v1.8.0): Loading from cache - Installing hamcrest/hamcrest-php (v2.0.0): Loading from cache - Installing mockery/mockery (1.2.1): Loading from cache - Installing filp/whoops (2.3.1): Loading from cache - Installing nunomaduro/collision (v2.1.1): Loading from cache - Installing sebastian/version (2.0.1): Loading from cache - Installing sebastian/resource-operations (2.0.1): Loading from cache - Installing sebastian/object-reflector (1.1.1): Loading from cache - Installing sebastian/recursion-context (3.0.0): Loading from cache - Installing sebastian/object-enumerator (3.0.3): Loading from cache - Installing sebastian/global-state (2.0.0): Loading from cache - Installing sebastian/exporter (3.1.0): Loading from cache - Installing sebastian/environment (4.1.0): Loading from cache - Installing sebastian/diff (3.0.2): Loading from cache - Installing sebastian/comparator (3.0.2): Loading from cache - Installing phpunit/php-timer (2.0.0): Loading from cache - Installing phpunit/php-text-template (1.2.1): Loading from cache - Installing phpunit/php-file-iterator (2.0.2): Loading from cache - Installing theseer/tokenizer (1.1.0): Loading from cache - Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache - Installing phpunit/php-token-stream (3.0.1): Loading from cache - Installing phpunit/php-code-coverage (6.1.4): Loading from cache - Installing doctrine/instantiator (1.1.0): Loading from cache - Installing webmozart/assert (1.4.0): Loading from cache - Installing phpdocumentor/reflection-common (1.0.1): Loading from cache - Installing phpdocumentor/type-resolver (0.4.0): Loading from cache - Installing phpdocumentor/reflection-docblock (4.3.0): Loading from cache - Installing phpspec/prophecy (1.8.0): Loading from cache - Installing phar-io/version (2.0.1): Loading from cache - Installing phar-io/manifest (1.0.3): Loading from cache - Installing myclabs/deep-copy (1.8.1): Loading from cache - Installing phpunit/phpunit (7.5.4): Loading from cache symfony/routing suggests installing doctrine/annotations (For using the annotation loader) symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader) symfony/routing suggests installing symfony/dependency-injection (For loading routes from a service) symfony/routing suggests installing symfony/expression-language (For using expression matching) symfony/routing suggests installing symfony/yaml (For using the YAML loader) symfony/contracts suggests installing psr/cache (When using the Cache contracts) symfony/contracts suggests installing symfony/cache-contracts-implementation symfony/contracts suggests installing symfony/service-contracts-implementation symfony/event-dispatcher suggests installing symfony/dependency-injection symfony/http-kernel suggests installing symfony/browser-kit symfony/http-kernel suggests installing symfony/config symfony/http-kernel suggests installing symfony/dependency-injection symfony/console suggests installing symfony/lock swiftmailer/swiftmailer suggests installing true/punycode (Needed to support internationalized email addresses, if ext-intl is not installed) paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.) ramsey/uuid suggests installing ircmaxell/random-lib (Provides RandomLib for use with the RandomLibAdapter) ramsey/uuid suggests installing ext-libsodium (Provides the PECL libsodium extension for use with the SodiumRandomGenerator) ramsey/uuid suggests installing ext-uuid (Provides the PECL UUID extension for use with the PeclUuidTimeGenerator and PeclUuidRandomGenerator) ramsey/uuid suggests installing moontoast/math (Provides support for converting UUID to 128-bit integer (in string form).) ramsey/uuid suggests installing ramsey/uuid-doctrine (Allows the use of Ramsey\Uuid\Uuid as Doctrine field type.) ramsey/uuid suggests installing ramsey/uuid-console (A console application for generating UUIDs with ramsey/uuid) symfony/translation suggests installing symfony/config symfony/translation suggests installing symfony/yaml nesbot/carbon suggests installing friendsofphp/php-cs-fixer (Needed for the `composer phpcs` command. Allow to automatically fix code style.) nesbot/carbon suggests installing phpstan/phpstan (Needed for the `composer phpstan` command. Allow to detect potential errors.) monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB) monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server) monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required)) monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server) monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server) monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver) monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib) monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome) monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar) monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server) monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server) league/flysystem suggests installing league/flysystem-aws-s3-v2 (Allows you to use S3 storage with AWS SDK v2) league/flysystem suggests installing league/flysystem-aws-s3-v3 (Allows you to use S3 storage with AWS SDK v3) league/flysystem suggests installing league/flysystem-azure (Allows you to use Windows Azure Blob storage) league/flysystem suggests installing league/flysystem-cached-adapter (Flysystem adapter decorator for metadata caching) league/flysystem suggests installing league/flysystem-eventable-filesystem (Allows you to use EventableFilesystem) league/flysystem suggests installing league/flysystem-rackspace (Allows you to use Rackspace Cloud Files) league/flysystem suggests installing league/flysystem-sftp (Allows you to use SFTP server storage via phpseclib) league/flysystem suggests installing league/flysystem-webdav (Allows you to use WebDAV storage) league/flysystem suggests installing league/flysystem-ziparchive (Allows you to use ZipArchive adapter) league/flysystem suggests installing spatie/flysystem-dropbox (Allows you to use Dropbox storage) league/flysystem suggests installing srmklive/flysystem-dropbox-v2 (Allows you to use Dropbox storage for PHP 5 applications) laravel/framework suggests installing aws/aws-sdk-php (Required to use the SQS queue driver and SES mail driver (^3.0).) laravel/framework suggests installing doctrine/dbal (Required to rename columns and drop SQLite columns (^2.6).) laravel/framework suggests installing league/flysystem-aws-s3-v3 (Required to use the Flysystem S3 driver (^1.0).) laravel/framework suggests installing league/flysystem-cached-adapter (Required to use the Flysystem cache (^1.0).) laravel/framework suggests installing league/flysystem-rackspace (Required to use the Flysystem Rackspace driver (^1.0).) laravel/framework suggests installing league/flysystem-sftp (Required to use the Flysystem SFTP driver (^1.0).) laravel/framework suggests installing moontoast/math (Required to use ordered UUIDs (^1.1).) laravel/framework suggests installing pda/pheanstalk (Required to use the beanstalk queue driver (^3.0).) laravel/framework suggests installing predis/predis (Required to use the redis cache and queue drivers (^1.0).) laravel/framework suggests installing pusher/pusher-php-server (Required to use the Pusher broadcast driver (^3.0).) laravel/framework suggests installing symfony/dom-crawler (Required to use most of the crawler integration testing tools (^4.1).) laravel/framework suggests installing symfony/psr-http-message-bridge (Required to psr7 bridging features (^1.0).) lcobucci/jwt suggests installing mdanter/ecc (Required to use Elliptic Curves based algorithms.) psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.) psy/psysh suggests installing hoa/console (A pure PHP readline implementation. You'll want this if your PHP install doesn't already support readline or libedit.) filp/whoops suggests installing whoops/soap (Formats errors as SOAP responses) sebastian/global-state suggests installing ext-uopz (*) phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0) phpunit/phpunit suggests installing phpunit/php-invoker (^2.0) phpunit/phpunit suggests installing ext-xdebug (*) Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel-dump-server Discovered Package: fideloper/proxy Discovered Package: laravel/nexmo-notification-channel Discovered Package: laravel/slack-notification-channel Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. > @php artisan key:generate --ansi Application key set successfully. vagrant@homestead:~/projects$

    Редактирование настроек для нового приложения:

    /home/yo/Homestead/Homestead.yaml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

      
    ...
      
    sites:
        - 
    maptask2.test
          to
    : /home/vagrant/projects/task2/public

      
    databases:
        - 
    task2_db
      
    ...
      
    /home/yo/projects/task2/.env:
    1
    2
    3
    4
    5

      
    ...
      
    DB_DATABASE=task2_db
      
    ...
      
    /etc/hosts:
    1
    2
    3
    4

      
    ...
      
    192.168.10.10 task2.test
      

    Перезагрузка виртуальной машины:

    bash:
    vagrant@homestead:~/projects$ exit logout Connection to 127.0.0.1 closed. yo@yo-Lenovo-G510:~/Homestead$ vagrant up --provision Bringing machine 'homestead-7' up with 'virtualbox' provider... ==> homestead-7: Checking if box 'laravel/homestead' version '7.1.0' is up to date... ==> homestead-7: Running provisioner: file... homestead-7: /home/yo/Homestead/aliases => /tmp/bash_aliases ==> homestead-7: Running provisioner: shell... homestead-7: Running: inline script ==> homestead-7: Running provisioner: shell... homestead-7: Running: inline script ==> homestead-7: Running provisioner: shell... homestead-7: Running: inline script ==> homestead-7: Running provisioner: shell... homestead-7: Running: /tmp/vagrant-shell20190210-19822-1ajyfoq.sh ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Certificate: laravel.test ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Site: laravel.test ==> homestead-7: Running provisioner: shell... homestead-7: Running: inline script ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Checking for old Schedule ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Certificate: laravel.scratch ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Site: laravel.scratch ==> homestead-7: Running provisioner: shell... homestead-7: Running: inline script ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Checking for old Schedule ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Certificate: task2.test ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Site: task2.test ==> homestead-7: Running provisioner: shell... homestead-7: Running: inline script ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Checking for old Schedule ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Clear Variables ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Restarting Cron ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Restarting Nginx ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating MySQL Database: homestead ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Postgres Database: homestead ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating MySQL Database: scratch ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Postgres Database: scratch ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating MySQL Database: task2_db ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Creating Postgres Database: task2_db ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Update Composer homestead-7: You are already using composer version 1.8.3 (stable channel). ==> homestead-7: Running provisioner: shell... homestead-7: Running: /tmp/vagrant-shell20190210-19822-6kuaum.sh ==> homestead-7: Running provisioner: shell... homestead-7: Running: script: Update motd ==> homestead-7: Running provisioner: shell... homestead-7: Running: /tmp/vagrant-shell20190210-19822-1vn731h.sh yo@yo-Lenovo-G510:~/Homestead$ vagrant ssh Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64) _ _ _ | | | | | | | |__ ___ _ __ ___ ___ ___| |_ ___ __ _ __| | | '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` | | | | | (_) | | | | | | __/\__ \ || __/ (_| | (_| | |_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_| * Homestead 8.0.0 released! PHP 7.3 is now the default! * Settler v7.0.0 released! Make sure you update * Need PHP 5.6 or 7.0? Homestead 7.x Settler 6.4.0 * Canonical Livepatch is available for installation. - Reduce system reboots and improve kernel security. Activate at: https://ubuntu.com/livepatch 0 packages can be updated. 0 updates are security updates. Last login: Sat Feb 9 19:54:44 2019 from 10.0.2.2 vagrant@homestead:~$ cd projects/task2 vagrant@homestead:~/projects/task2$
    bash:
    vagrant@homestead:~/projects/task2$ php artisan make:auth Authentication scaffolding generated successfully. vagrant@homestead:~/projects/task2$ php artisan migrate Nothing to migrate. vagrant@homestead:~/projects/task2$ vagrant@homestead:~/projects/task2$ php artisan make:auth Authentication scaffolding generated successfully. vagrant@homestead:~/projects/task2$ php artisan migrate Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table vagrant@homestead:~/projects/task2$

    Добавим 4 таблицы: roles, permissions, role2user, и permissions2role:

    bash:
    vagrant@homestead:~/projects/task2$ php artisan make:migration createRolesTable Created Migration: 2019_02_10_212326_create_roles_table vagrant@homestead:~/projects/task2$ php artisan make:migration createPermissionsTable Created Migration: 2019_02_10_212445_create_permissions_table vagrant@homestead:~/projects/task2$ php artisan make:migration createRolesToPermissionsTable Created Migration: 2019_02_10_212928_create_roles_to_permissions_table vagrant@homestead:~/projects/task2$ php artisan make:migration createUsersToRolesTable Created Migration: 2019_02_10_213035_create_users_to_roles_table vagrant@homestead:~/projects/task2$

    и опишем их структуру:

    /home/yo/projects/task2/database/migrations/2019_02_10_212326_create_roles_table.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

      
    <?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->string('label');
            
    $table->text('description');
            
    $table->timestamps();
          });
        }

        
    /**
         * Reverse the migrations.
         *
         * @return void
         */
        
    public function down()
        {
          
    Schema::dropIfExists('roles');
        }
      }
      
    /home/yo/projects/task2/database/migrations/2019_02_10_212445_create_permissions_table.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

      
    <?php

      
    use Illuminate\Support\Facades\Schema;
      use 
    Illuminate\Database\Schema\Blueprint;
      use 
    Illuminate\Database\Migrations\Migration;

      class 
    CreatePermissionsTable extends Migration
      
    {
        
    /**
         * Run the migrations.
         *
         * @return void
         */
        
    public function up()
        {
          
    Schema::create('permissions', function (Blueprint $table) {
            
    $table->increments('id');
            
    $table->string('name');
            
    $table->string('label');
            
    $table->text('description');
            
    $table->timestamps();
          });
        }

        
    /**
         * Reverse the migrations.
         *
         * @return void
         */
        
    public function down()
        {
          
    Schema::dropIfExists('permissions');
        }
      }
      
    /home/yo/projects/task2/database/migrations/2019_02_10_213035_create_users_to_roles_table.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

      
    <?php

      
    use Illuminate\Support\Facades\Schema;
      use 
    Illuminate\Database\Schema\Blueprint;
      use 
    Illuminate\Database\Migrations\Migration;

      class 
    CreateUsersToRolesTable extends Migration
      
    {
        
    /**
         * Run the migrations.
         *
         * @return void
         */
        
    public function up()
        {
          
    Schema::create('users_to_roles', function (Blueprint $table) {
            
    $table->increments('id');
            
    $table->integer('role_id');
            
    $table->integer('user_id');
            
    $table->timestamps();
          });
        }

        
    /**
         * Reverse the migrations.
         *
         * @return void
         */
        
    public function down()
        {
          
    Schema::dropIfExists('users_to_roles');
        }
      }
      
    /home/yo/projects/task2/database/migrations/2019_02_10_212928_create_roles_to_permissions_table.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

      
    <?php

      
    use Illuminate\Support\Facades\Schema;
      use 
    Illuminate\Database\Schema\Blueprint;
      use 
    Illuminate\Database\Migrations\Migration;

      class 
    CreateRolesToPermissionsTable extends Migration
      
    {
        
    /**
         * Run the migrations.
         *
         * @return void
         */
        
    public function up()
        {
          
    Schema::create('roles_to_permissions', function (Blueprint $table) {
            
    $table->increments('id');
            
    $table->integer('permissions_id');
            
    $table->integer('role_id');
            
    $table->timestamps();
          });
        }

        
    /**
         * Reverse the migrations.
         *
         * @return void
         */
        
    public function down()
        {
          
    Schema::dropIfExists('roles_to_permissions');
        }
      }
      
    bash:
    vagrant@homestead:~/projects/task2$ php artisan migrate Migrating: 2019_02_10_212326_create_roles_table Migrated: 2019_02_10_212326_create_roles_table Migrating: 2019_02_10_212445_create_permissions_table Migrated: 2019_02_10_212445_create_permissions_table Migrating: 2019_02_10_212928_create_roles_to_permissions_table Migrated: 2019_02_10_212928_create_roles_to_permissions_table Migrating: 2019_02_10_213035_create_users_to_roles_table Migrated: 2019_02_10_213035_create_users_to_roles_table vagrant@homestead:~/projects/task2$
    bash:
    vagrant@homestead:~/projects/task2$ php artisan make:model Role Model created successfully. vagrant@homestead:~/projects/task2$ php artisan make:model Permission Model created successfully. vagrant@homestead:~/projects/task2$ php artisan make:model PermissionsToRoles Model created successfully. vagrant@homestead:~/projects/task2$ php artisan make:model UsersToRoles Model created successfully. vagrant@homestead:~/projects/task2$
    bash:
    vagrant@homestead:~/projects/task2$ php artisan make:seeder RoleSeeder Seeder created successfully. vagrant@homestead:~/projects/task2$ php artisan make:seeder UserSeeder Seeder created successfully. vagrant@homestead:~/projects/task2$ php artisan make:seeder PermissionSeeder Seeder created successfully. vagrant@homestead:~/projects/task2$
    /home/yo/projects/task2/database/seeds/UserSeeder.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32

      
    <?php

      
    use Illuminate\Database\Seeder;

      class 
    UserSeeder extends Seeder
      
    {
        
    /**
         * Run the database seeds.
         *
         * @return void
         */
        
    public function run()
        {
          
    $users = [
            [
    'Administrator''admin@gmail.com',],
            [
    'User01''user01@gmail.com',],
            [
    'User02''user02@gmail.com',],
            [
    'User03''user03@gmail.com',],
            [
    'User04''user04@gmail.com',],
            [
    'User05''user05@gmail.com',],
          ];
          foreach(
    $users as $user){
            
    DB::table('users')->insert([
              
    'name' => $user[0],
              
    'email' => $user[1],
              
    'password' => bcrypt('111111'),
            ]);
          }
        }
      }
      
    /home/yo/projects/task2/database/seeds/RoleSeeder.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

      
    <?php

      
    use Illuminate\Database\Seeder;

      class 
    RoleSeeder extends Seeder
      
    {
        
    /**
         * Run the database seeds.
         *
         * @return void
         */
        
    public function run()
        {
          
    $roles = [
            [
    'owner''application owner''description of the role of the owner',],
            [
    'admin''application administrator''description of the role of the administrator',],
            [
    'user''application user''description of the role of the user',],
          ];
          foreach(
    $roles as $role){
            
    DB::table('roles')->insert([
              
    'name' => $role[0],
              
    'label' => $role[1],
              
    'description' => $role[2],
            ]);
          }
        }
      }
      
    /home/yo/projects/task2/database/seeds/PermissionSeeder.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

      
    <?php

      
    use Illuminate\Database\Seeder;

      class 
    PermissionSeeder extends Seeder
      
    {
        
    /**
         * Run the database seeds.
         *
         * @return void
         */
        
    public function run()
        {
          
    $permissions = [
            [
    'created''label created permission''description created permission',],
            [
    'modify''label modify permission''description modify permission',],
            [
    'delete''label delete permission''description delete permission',],
          ];
          foreach(
    $permissions as $permission){
            
    DB::table('permissions')->insert([
              
    'name' => $permission[0],
              
    'label' => $permission[1],
              
    'description' => $permission[2],
            ]);
          }
        }
      }
      
    bash:
    vagrant@homestead:~/projects/task2$ php artisan db:seed --class=UserSeeder Database seeding completed successfully. vagrant@homestead:~/projects/task2$ php artisan db:seed --class=RoleSeeder Database seeding completed successfully. vagrant@homestead:~/projects/task2$ php artisan db:seed --class=PermissionSeeder Database seeding completed successfully. vagrant@homestead:~/projects/task2$ mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 142 Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | homestead | | laravel_db | | mysql | | performance_schema | | scratch | | scratch_db | | sys | | task2_db | +--------------------+ 9 rows in set (0.00 sec) mysql> use task2_db; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------+ | Tables_in_task2_db | +----------------------+ | migrations | | password_resets | | permissions | | roles | | roles_to_permissions | | users | | users_to_roles | +----------------------+ 7 rows in set (0.00 sec) mysql> select * from users; +----+---------------+------------------+-------------------+--------------------------------------------------------------+----------------+------------+------------+ | id | name | email | email_verified_at | password | remember_token | created_at | updated_at | +----+---------------+------------------+-------------------+--------------------------------------------------------------+----------------+------------+------------+ | 1 | Administrator | admin@gmail.com | NULL | $2y$10$MC4zk/Ba8oVHopWP9/UfzevkDB7iS.xxQM9BXx48us.Ry/SqgLVS. | NULL | NULL | NULL | | 2 | User01 | user01@gmail.com | NULL | $2y$10$WNafvTM5RzCGgkXKyGtfHu.TvG0TUTvbMtggUipQJNsWmhv3SRBWC | NULL | NULL | NULL | | 3 | User02 | user02@gmail.com | NULL | $2y$10$o.7NRdhjeY75IpUfiLVE..Ch7t7z6k6L5bhV4L/IJtKkxYVKxQ1Hq | NULL | NULL | NULL | | 4 | User03 | user03@gmail.com | NULL | $2y$10$9502G9hnFkuQtyXClA1e9uDToiHoJbyy/Tpuqqa8Je2or7N1gmt96 | NULL | NULL | NULL | | 5 | User04 | user04@gmail.com | NULL | $2y$10$F52i81T4o1Ww.DjI/dwAy.AYcHLfN.710r2pyWqadXLjPcqk0Vvsi | NULL | NULL | NULL | | 6 | User05 | user05@gmail.com | NULL | $2y$10$4t20vWfDvAtcGfNwDmpTQOBBcce9N.zPhbkAYzTQS6MOaqJtWYXgq | NULL | NULL | NULL | +----+---------------+------------------+-------------------+--------------------------------------------------------------+----------------+------------+------------+ 6 rows in set (0.00 sec) mysql> select * from roles; +----+-------+---------------------------+----------------------------------------------+------------+------------+ | id | name | label | description | created_at | updated_at | +----+-------+---------------------------+----------------------------------------------+------------+------------+ | 1 | owner | application owner | description of the role of the owner | NULL | NULL | | 2 | admin | application administrator | description of the role of the administrator | NULL | NULL | | 3 | user | application user | description of the role of the user | NULL | NULL | +----+-------+---------------------------+----------------------------------------------+------------+------------+ 3 rows in set (0.00 sec) mysql> select * from permissions; +----+---------+--------------------------+--------------------------------+------------+------------+ | id | name | label | description | created_at | updated_at | +----+---------+--------------------------+--------------------------------+------------+------------+ | 1 | created | label created permission | description created permission | NULL | NULL | | 2 | modify | label modify permission | description modify permission | NULL | NULL | | 3 | delete | label delete permission | description delete permission | NULL | NULL | +----+---------+--------------------------+--------------------------------+------------+------------+ 3 rows in set (0.00 sec) mysql> exit Bye vagrant@homestead:~/projects/task2$
    bash:
    vagrant@homestead:~/projects/task2$ php artisan make:controller UserController Controller created successfully. vagrant@homestead:~/projects/task2$ php artisan make:controller RoleController Controller created successfully. vagrant@homestead:~/projects/task2$ php artisan make:controller PermissionController Controller created successfully. vagrant@homestead:~/projects/task2$
    /home/yo/projects/task2/app/Http/Controllers/UserController.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

      
    <?php

      
    namespace App\Http\Controllers;

      use 
    Illuminate\Http\Request;

      class 
    UserController extends Controller
      
    {
        
    /**
         * Create a new controller instance.
         *
         * @return void
         */
        
    public function __construct()
        {
          
    $this->middleware('auth');
        }

        
    /**
         * Show the application dashboard.
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        
    public function index()
        {
          return 
    view('users');
        }
      }
      
    /home/yo/projects/task2/app/Http/Controllers/RoleController.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84

      
    <?php

      
    namespace App\Http\Controllers;

      use 
    Illuminate\Http\Request;
      use 
    App\Role;

      class 
    RoleController extends Controller
      
    {
        
    /**
         * Create a new controller instance.
         *
         * @return void
         */
        
    public function __construct()
        {
          
    $this->middleware('auth');
        }

        
    /**
         * Show the application dashboard.
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        
    public function index(){
          
    // return view('role');

          
    if(!empty(request('id'))){
            
    // $role='updating role id'.request('id');
            
    $role Role::where('id'request('id'))->first();
          }else{
            
    $role=false;
          }

          
    $roles Role::all();
          return 
    view('roles'compact('role''roles'));
        }


        
    /**
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        
    public function save(){
          
    $role=new Role;
          
    $role->name=request('name');
          
    $role->label=request('label');
          
    $role->description=request('description');
          
    $role->save();

          return 
    redirect('/roles');
        }


        
    /**
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        
    public function update(){
          
    $role Role::where('id'request('id'))->first();
          
    $role->id=request('id');
          
    $role->name=request('name');
          
    $role->label=request('label');
          
    $role->description=request('description');
          
    $role->save();

          return 
    redirect('/roles');
        }


        
    /**
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        
    public function delete(){
          
    $role Role::where('id'request('id'))->first();
          
    $role->delete();

          return 
    redirect('/roles');
        }

      }
      
    /home/yo/projects/task2/app/Http/Controllers/PermissionController.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

      
    <?php

      
    namespace App\Http\Controllers;

      use 
    Illuminate\Http\Request;

      class 
    PermissionController extends Controller
      
    {
        
    /**
         * Create a new controller instance.
         *
         * @return void
         */
        
    public function __construct()
        {
          
    $this->middleware('auth');
        }

        
    /**
         * Show the application dashboard.
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        
    public function index()
        {
          return 
    view('permissions');
        }
      }
      
    /home/yo/projects/task2/resources/views/layouts/admlayer.blade.php:
    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225

      
    <!doctype html>
      <
    html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <
    head>
          <
    meta charset="utf-8">
          <
    meta name="viewport" content="width=device-width, initial-scale=1">

          <!-- 
    CSRF Token -->
          <
    meta name="csrf-token" content="{{ csrf_token() }}">

          <
    title>{{ config('app.name''Laravel') }}</title>

          <!-- 
    Scripts -->
          <
    script src="{{ asset('js/app.js') }}" defer></script>

          <!-- 
    Fonts -->
          <
    link rel="dns-prefetch" href="//fonts.gstatic.com">
          <
    link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">

          <!-- 
    Styles -->
          <
    link href="{{ asset('css/app.css') }}" rel="stylesheet">

          <!-- 
    Styles -->
          <
    style>
            
    htmlbody {
              
    background-color#fff;
              
    color#636b6f;
              
    font-family'Nunito'sans-serif;
              
    font-weight200;
              
    height100vh;
              
    margin0;
            }

            .
    full-height {
              
    height100vh;
            }

            .
    flex-center {
              
    align-itemscenter;
              
    displayflex;
              
    justify-contentcenter;
            }

            .
    position-ref {
              
    positionrelative;
            }

            .
    top-right {
              
    positionabsolute;
              
    right10px;
              
    top18px;
            }

            .
    content {
              
    text-aligncenter;
            }

            .
    title {
              
    font-size84px;
            }

            .
    links {
              
    color#636b6f;
              
    padding0 25px;
              
    font-size13px;
              
    font-weight600;
              
    letter-spacing.1rem;
              
    text-decorationnone;
              
    text-transformuppercase;
            }

            .
    m-b-md {
              
    margin-bottom30px;
            }



            
    /* from welcome */
            
    htmlbody {
              
    background-color#fff;
              
    color#636b6f;
              
    font-family'Nunito'sans-serif;
              
    font-weight200;
              
    height100vh;
              
    margin0;
            }

            .
    full-height {
              
    height100vh;
            }

            .
    flex-center {
              
    align-itemscenter;
              
    displayflex;
              
    justify-contentcenter;
            }

            .
    position-ref {
              
    positionrelative;
            }

            .
    top-right {
              
    positionabsolute;
              
    right10px;
              
    top18px;
            }

            .
    content {
              
    text-aligncenter;
            }

            .
    title {
              
    font-size84px;
            }

            .
    links {
              
    color#636b6f;
              
    padding0 25px;
              
    font-size13px;
              
    font-weight600;
              
    letter-spacing.1rem;
              
    text-decorationnone;
              
    text-transformuppercase;
            }

            .
    m-b-md {
              
    margin-bottom30px;
            }
            
    /* from welcome */


            
    .yoform{
               
    margin-bottom3em;
            }

            .
    tr_t{
              
    width:100%;
              
    border-collapsecollapse;
              
    border-spacing0;
              
    margin-bottom3em;
            }
            .
    tr_t td, .tr_t th{
              
    text-align:left;
              
    padding:5px;
              
    border:1px solid #bbb;
            
    }

            
    form.update inputform.delete inputinput.updateinput.delete{
              
    width:5em;
              
    color:#888;
              
    padding:0px 3px;
              
    margin:3px;
            }


          </
    style>
        </
    head>

        <
    body>
          <
    div id="app">
            <
    nav class="navbar navbar-expand-md navbar-light navbar-laravel">
              <
    div class="container">
                
    Dashboard {{ config('app.name''Laravel') }}
                <
    button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
                  <
    span class="navbar-toggler-icon"></span>
                </
    button>

                <
    div class="collapse navbar-collapse" id="navbarSupportedContent">
                  <!-- 
    Left Side Of Navbar -->
                  <
    ul class="navbar-nav mr-auto">

                  </
    ul>

                  <!-- 
    Right Side Of Navbar -->
                  <
    ul class="navbar-nav ml-auto">
                    <!-- 
    Authentication Links -->
                    @
    guest
                      
    <li class="nav-item">
                        <
    class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                      </
    li>
                      @if (
    Route::has('register'))
                        <
    li class="nav-item">
                          <
    class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                        </
    li>
                      @endif
                    @else
                      <
    li class="nav-item dropdown">
                        <
    a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                          
    you are logged as {{ Auth::user()->name }} <span class="caret"></span>
                        </
    a>

                        <
    div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                          <
    class="dropdown-item" href="{{ route('logout') }}"
                          
    onclick="event.preventDefault();
                                  document.getElementById('logout-form').submit();"
    >
                            {{ 
    __('Logout') }}
                          </
    a>

                          <
    form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                            @
    csrf
                          
    </form>
                        </
    div>
                      </
    li>
                    @
    endguest
                  
    </ul>
                </
    div>
              </
    div>
            </
    nav>

            <
    main class="py-4">
              @yield(
    'content')
            </
    main>

            <
    div class="links content">
              <
    a href="/users">User</a>
              <
    a href="/roles">Role</a>
              <
    a href="/permissions">Permission</a>
              <
    a href="/home">Home</a>
              <
    a href="/">Welcome</a>
            </
    div>
          </
    div>
        </
    body>

      </
    html>
      
    patch:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92

      
    @extends('layouts.admlayer')

      @
    section('content')
      <
    div class="container">
        <
    div class="row justify-content-center">
          <
    div class="col-md-12">
            <!--
    div class="card">
              <
    div class="card-header">Dashboard</div>

              <
    div class="card-body">
                @if (
    session('status'))
                  <
    div class="alert alert-success" role="alert">
                    {{ 
    session('status') }}
                  </
    div>
                @endif

                
    You are logged in!
              </
    div>
            </
    div-->




            @if (
    $role)
            <
    h2>Update Role {{ $role->name }}</h2>
            <
    form action='/roles/update' method='post' class='yoform'>
              {{ 
    csrf_field() }}
              <
    input type='hidden' name='id' value='{{ $role->id }}'>
              
    name: <input type='text' name='name' value='{{ $role->name }}'>
              
    label: <input type='text' name='label' value='{{ $role->label }}'>
              
    description: <input type='text' name='description' value='{{ $role->description }}'>
              <
    input type='submit' value='upd'>
            </
    form>
            @endif


            <
    h2>List Roles</h2>
            <
    table class='tr_t'>
              <
    tr>
                <
    th>id</th>
                <
    th>name</th>
                <
    th>label</th>
                <
    th>description</th>
                <
    th>created_at</th>
                <
    th>updated_at</th>
                <
    th>action</th>
              </
    tr>
            @foreach(
    $roles as $role)
              <
    tr>
                <
    td>{{ $role->id }}</td>
                <
    td>{{ $role->name }}</td>
                <
    td>{{ $role->label }}</td>
                <
    td>{{ $role->description }}</td>
                <
    td>{{ $role->created_at }}</td>
                <
    td>{{ $role->updated_at }}</td>
                <
    td>
                  <
    input type='submit' value='upd' form="update_form" class='update'>
                  <
    input type='submit' value='del' form="delete_form" class='delete'>

                  <
    form id='update_form' action='/roles/edit' method='post' class='update'>
                    {{ 
    csrf_field() }}
                    <
    input type='hidden' name='id' value='{{ $role->id }}'>
                    <!--
    input type='submit' value='upd'-->
                  </
    form>
                  <
    form id='delete_form' action='/roles/delete' method='post' class='delete'>
                    {{ 
    csrf_field() }}
                    <
    input type='hidden' name='id' value='{{ $role->id }}'>
                    <!--
    input type='submit' value='del'-->
                  </
    form>
                </
    td>
              </
    tr>
            @endforeach
            </
    table>

            
    выделить цветом редактируемую записьподтверждение удаления и вывести сообщения!

            <
    h2>Add new Role</h2>
            <
    form action='/roles' method='post' class='yoform'>
              {{ 
    csrf_field() }}
              
    name: <input type='text' name='name'>
              
    label: <input type='text' name='label'>
              
    description: <input type='text' name='description'>
              <
    input type='submit' value='add'>
            </
    form>


          </
    div>
        </
    div>
      </
    div>
      @
    endsection
      

    Прописываем основные роуты:

    /home/yo/projects/task2/routes/web.php:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

      
    ...
      
    // index
      
    Route::get('/users''UserController@index');
      
    Route::get('/roles''RoleController@index');
      
    Route::get('/permissions''PermissionController@index');

      
    // save
      
    Route::post('/users''UserController@save');
      
    Route::post('/roles''RoleController@save');
      
    Route::post('/permissions''PermissionController@save');

      
    // update
      
    Route::post('/users/edit''UserController@index');
      
    Route::post('/users/update''UserController@update');
      
    Route::post('/roles/edit''RoleController@index');
      
    Route::post('/roles/update''RoleController@update');
      
    Route::post('/permissions/edit''PermissionController@index');
      
    Route::post('/permissions/update''PermissionController@update');

      
    // delete
      
    Route::post('/users/delete''UserController@delete');
      
    Route::post('/roles/delete''RoleController@delete');
      
    Route::post('/permissions/delete''PermissionController@delete');