Создание интернет-витрины на laravel. Часть 5
Добавление категорий товаров.
Капустин Яков
оглавление
- 01 Добавление разрешений для категорий
- 02 Создание модели, миграции, контроллера и наполнителя (сидера)
01Добавление разрешений для категорий
upd! Исправлена ошибка в PermissionRoleTableSeeder: разрешения для всех ролей автоматом присваивались разрешения из последнего условия.
database/seeds/PermissionRoleTableSeeder.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
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
<?php
use Illuminate\Database\Seeder;
use App\Role;
use App\Permission;
class PermissionRoleTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$roles = Role::all();
$permissions = Permission::all();
foreach ($roles as $role) {
foreach ($permissions as $permission) {
if (
// for owner
$role->id == 1
or
// for admin
$role->id == 2 and !in_array( $permission->id, [1, 2, 3, 5, 6, 7, 21, 22, 23] )
or
// for manager
$role->id == 3 and ( $permission->id > 13 and !in_array( $permission->id, [15, 19, 21, 22, 23] ) )
or
// for user
$role->id == 4 and in_array( $permission->id, [16, 20] )
) {
DB::table('permission_role')->insert([
'permission_id' => $permission->id,
'role_id' => $role->id,
]);
}
}
}
}
}
database/seeds/PermissionsTableSeeder.php:
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
...
// permissions for categories
/* 21 */ ['group' => 'categories', 'name' => 'create_categories', 'display_name' => 'Create Categories', 'description' => 'Create the Product', ],
/* 22 */ ['group' => 'categories', 'name' => 'edit_categories', 'display_name' => 'Edit Categories', 'description' => 'Edit the Product', ],
/* 23 */ ['group' => 'categories', 'name' => 'delete_categories', 'display_name' => 'Delete Categories', 'description' => 'Delete the Product', ],
/* 24 */ ['group' => 'categories', 'name' => 'view_categories', 'display_name' => 'View Categories', 'description' => 'View the Product', ],
...
02Создание модели, миграции, контроллера и наполнителя (сидера)
Добавим тип продукта (внесем изменения в database/migrations/2019_05_17_163750_create_products_table.php, database/seeds/ProductsTableSeeder.php, resources/views/products/show.blade.php, resources/views/products/create.blade.php и resources/views/products/edit.blade.php).
И создадим модели, миграции и прочее.
bash:vagrant@homestead:~/projects/kk$ php artisan make:model Category -c -m Model created successfully. Created Migration: 2019_05_31_075558_create_categories_table Controller created successfully. vagrant@homestead:~/projects/kk$ php artisan make:seeder CategoriesTableSeeder Seeder created successfully. vagrant@homestead:~/projects/kk$ vagrant@homestead:~/projects/kk$ vagrant@homestead:~/projects/kk$ vagrant@homestead:~/projects/kk$ php artisan make:migration create_category_product_table Created Migration: 2019_05_31_081031_create_category_product_table vagrant@homestead:~/projects/kk$ php artisan make:seeder CategoryProductTableSeeder Seeder created successfully. vagrant@homestead:~/projects/kk$ php artisan make:model CategoryProduct Model created successfully. vagrant@homestead:~/projects/kk$
В модели опишем отношения категорий с товарами.
app/Category.php:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Product;
class Category extends Model
{
public function getProduct() {
return $this->hasMany(Product::class);
}
}
nameFile:
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
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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('description')->nullable();
$table->string('image')->nullable()->charset('utf8');
$table->unsignedInteger('added_by_user_id');
$table->unsignedInteger('edited_by_user_id')->nullable();
$table->boolean('top_level_menu');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
nameFile:
1
2
2
nameFile:
1
2
2
nameFile:
1
2
2
.
E717: отсутствует исходный файл изображения 'nameFile.png';
Капустин Яков (2019.05.31 09:43)