diff --git a/app/Http/Controllers/EloquentController.php b/app/Http/Controllers/EloquentController.php index b4e64c0..582129b 100644 --- a/app/Http/Controllers/EloquentController.php +++ b/app/Http/Controllers/EloquentController.php @@ -12,7 +12,7 @@ public function task2() // TODO Eloquent Задание 2: С помощью модели Item реализовать запрос в переменной products // select * from products where active = true order by created_at desc limit 3 // вместо [] - $products = []; + $products = Item::where('active',true)->orderBy('created_at','DESC')->take(3)->get(); return view('eloquent.task2', [ 'products' => $products @@ -36,17 +36,23 @@ public function task4($id) // TODO Eloquent Задание 4: Найти Item по id и передать во view либо отдать 404 страницу // Одна строка кода // вместо [] - $product = []; + $product = Item::whereid($id)->first(); + if($product){ + return view('eloquent.task4', [ + 'product' => $product + ]); + } + else{ + abort(404); + } - return view('eloquent.task4', [ - 'product' => $product - ]); } public function task5(Request $request) { // TODO Eloquent Задание 5: В запросе будет все необходимое для создания записи // Выполнить простое добавление новой записи в Item на основе $request + Item::create($request->except('crsf_token')); return redirect('/'); } @@ -54,6 +60,7 @@ public function task5(Request $request) public function task6($id, Request $request) { $product = Item::findOrFail($id); + $product->update($request->except('crsf_token')); // TODO Eloquent Задание 6: В запросе будет все необходимое для обновления записи // Выполнить простое обновление записи на основе $request @@ -62,9 +69,10 @@ public function task6($id, Request $request) public function task7(Request $request) { + // TODO Eloquent Задание 7: В запросе будет параметр products который будет содержать массив с id // [1,2,3,4 ...] выполнить массовое удаление записей модели Item с учетом id в $request - + $product = Item::whereIn('id',$request->input("products"))->delete(); return redirect('/'); } } diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 98f291d..365fc60 100644 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -13,6 +13,7 @@ public function index() return view('welcome', [ 'title' => 'Welcome', + 'users'=>$users, // TODO Blade Задание 1: Передайте users во view (название ключа users) ]); } diff --git a/app/Http/Requests/ItemStoreRequest.php b/app/Http/Requests/ItemStoreRequest.php index d921832..840a79d 100644 --- a/app/Http/Requests/ItemStoreRequest.php +++ b/app/Http/Requests/ItemStoreRequest.php @@ -24,6 +24,7 @@ public function authorize() public function rules() { return [ + 'title'=>'required|string|min:5|max:15', //TODO Validation Задание: Добавить правила валидации для поля title // Поле обязательно // Строковое diff --git a/app/Models/Category.php b/app/Models/Category.php index 5a6d1ff..5bf78dc 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -4,10 +4,21 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; +use App\Models\article; class Category extends Model { - use HasFactory; - + use HasFactory,SoftDeletes; protected $fillable = ['title']; + + /** + * The article t + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function article() + { + return $this->belongsToMany(article::class, 'article_category', 'article_id', 'category_id'); + } } diff --git a/app/Models/Item.php b/app/Models/Item.php index 63d0a6c..6522a57 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -8,7 +8,7 @@ class Item extends Model { use HasFactory; - + protected $table='products'; protected $fillable = ['title', 'active']; // TODO Eloquent Задание 1: указать что таблица - products diff --git a/app/Policies/ItemPolicy.php b/app/Policies/ItemPolicy.php index dd7d720..63e8d95 100644 --- a/app/Policies/ItemPolicy.php +++ b/app/Policies/ItemPolicy.php @@ -42,8 +42,8 @@ public function view(User $user, Item $item) public function create(User $user) { // TODO Auth Задание: Разрешить добавление продуктов только пользователю с id = 10 - - return true; + if($user->id==10) return true; + return false; } /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index b179285..6dde049 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -16,6 +16,7 @@ class AppServiceProvider extends ServiceProvider public function register() { // + } /** @@ -25,6 +26,6 @@ public function register() */ public function boot() { - + Blade::component('hello', HelloWorld::class); } } diff --git a/app/View/Components/HelloWorld.php b/app/View/Components/HelloWorld.php new file mode 100644 index 0000000..948d2d7 --- /dev/null +++ b/app/View/Components/HelloWorld.php @@ -0,0 +1,28 @@ + Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, + // 'Hello'=>App\View\Components\HelloWorld::class, + ], ]; diff --git a/database/migrations/2022_04_11_114946_create_categories_table.php b/database/migrations/2022_04_11_114946_create_categories_table.php new file mode 100644 index 0000000..49a51be --- /dev/null +++ b/database/migrations/2022_04_11_114946_create_categories_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('title')->nullable()->default(null); + $table->text('description')->nullable(); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('categories'); + } +} diff --git a/database/migrations/2022_04_11_120058_create_articles_table.php b/database/migrations/2022_04_11_120058_create_articles_table.php new file mode 100644 index 0000000..0628b4a --- /dev/null +++ b/database/migrations/2022_04_11_120058_create_articles_table.php @@ -0,0 +1,36 @@ +id(); + $table->string('name')->nullable()->default(null); + $table->string('description'); + $table->boolean('active')->default(true); + $table->boolean('main')->default(false); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('articles'); + } +} diff --git a/database/migrations/2022_04_12_033316_article_category.php b/database/migrations/2022_04_12_033316_article_category.php new file mode 100644 index 0000000..fa5c159 --- /dev/null +++ b/database/migrations/2022_04_12_033316_article_category.php @@ -0,0 +1,34 @@ +id(); + $table->unsignedBigInteger('category_id'); + $table->unsignedBigInteger('article_id'); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/auth.blade.php b/resources/views/auth.blade.php index 25c606e..6aa2fe8 100644 --- a/resources/views/auth.blade.php +++ b/resources/views/auth.blade.php @@ -1,3 +1,6 @@ +@if(Auth::check()) + {{Auth::id()}} +@endif - \ No newline at end of file + diff --git a/resources/views/components/hello-world.blade.php b/resources/views/components/hello-world.blade.php new file mode 100644 index 0000000..57c0168 --- /dev/null +++ b/resources/views/components/hello-world.blade.php @@ -0,0 +1,3 @@ +
+ {{now()->format('Y-m-d')}} +
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index c23848d..c67b0b0 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -9,6 +9,7 @@ + @include('shared.menu') @yield('content') diff --git a/resources/views/table.blade.php b/resources/views/table.blade.php index 190cc19..5ed2b56 100644 --- a/resources/views/table.blade.php +++ b/resources/views/table.blade.php @@ -1,6 +1,6 @@ - +@extends('layouts.app') @@ -11,4 +11,16 @@ +{{-- @dd($data) --}} +@if($data->count()>0) +@foreach ($data as $key=>$item) + +
+ {{$item->name}} +
+@endforeach +@else +
Ничего не найдено +
+@endif diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 9bce66d..64845bd 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -20,15 +20,18 @@ } +

Welcome

+ +
diff --git a/routes/api.php b/routes/api.php index 2cf9c39..df88741 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,5 +1,6 @@ group(function () { + route::apiResource('users',UserController::class); + }); }); diff --git a/routes/web.php b/routes/web.php index 9842fb6..9bf7e47 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,52 +1,63 @@ name('contact'); //TODO Route Задание 4: По GET урлу /users/[id] обратиться к UserController, метод show // без Route Model Binding. Только параметр id // Одна строка кода - +Route::GET('/users/{id}',[UserController::class,'show'])->name('users.show'); //TODO Route Задание 5: По GET урлу /users/bind/[user] обратиться к UserController, метод showBind // но в данном случае используем Route Model Binding. Параметр user // Одна строка кода - +Route::get('/users/bind/{id}',[UserController::class,'showBind'])->name('users.showBind'); //TODO Route Задание 6: Выполнить редирект с урла /bad на урл /good // Одна строка кода - +Route::get('/bad', function () { + return redirect('/good'); +}); //TODO Route Задание 7: Добавить роут на ресурс контроллер - UserCrudController с урлом - /users_crud // Одна строка кода - +Route::resource('/users_crud', UserCrudController::class); //TODO Route Задание 8: Организовать группу роутов (Route::group()) объединенных префиксом - dashboard +Route::prefix('dashboard')->group(function () { // Задачи внутри группы роутов dashboard //TODO Route Задание 9: Добавить роут GET /admin -> Admin/IndexController -> index + Route::get('/admin',[AdminIndexController::class,'index']); //TODO Route Задание 10: Добавить роут POST /admin/post -> Admin/IndexController -> post - - + Route::post('/admin/post',[AdminIndexController::class,'post']); +}); //TODO Route Задание 11: Организовать группу роутов (Route::group()) объединенных префиксом - security и мидлваром auth - +Route::prefix('security')->middleware('auth')->group(function () { + Route::get('/admin/auth',[AdminIndexController::class,'auth']); +}); // Задачи внутри группы роутов security //TODO Задание 12: Добавить роут GET /admin/auth -> Admin/IndexController -> auth -require __DIR__ . '/default.php'; \ No newline at end of file +require __DIR__ . '/default.php'; diff --git a/tests/Feature/RouteTest.php b/tests/Feature/RouteTest.php index 53e077a..f141041 100644 --- a/tests/Feature/RouteTest.php +++ b/tests/Feature/RouteTest.php @@ -102,8 +102,8 @@ public function test_task_7() public function test_tasks_8_12() { $response = $this->get('/dashboard/admin'); + // dd($response->assertView()); $response->assertViewIs('welcome'); - $response = $this->post('/dashboard/admin/post'); $response->assertViewIs('welcome'); $response->assertStatus(200);