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 @@ +