質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel 6

Laravel 6は、オープンソースなPHPのフレームワーク。Webアプリケーションの開発に適しており、バージョン6はLTSです。5.8での向上に加えて、セマンティックバージョニングの採用やLaravel Vaporとのコンパチビリティなどが変更されています。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

0回答

578閲覧

Laravelでのフォーム機能でのカテゴリの書き方について

takigawa777

総合スコア21

Laravel 6

Laravel 6は、オープンソースなPHPのフレームワーク。Webアプリケーションの開発に適しており、バージョン6はLTSです。5.8での向上に加えて、セマンティックバージョニングの採用やLaravel Vaporとのコンパチビリティなどが変更されています。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2022/12/14 08:33

前提

Laravelでフリーマーケットサイトの作成をしているのですが、その中で出品する際に名前や価格と同じようにカテゴリを設定し、出品一覧画面に作る検索機能でカテゴリや名前を用いて検索がかけられるようにしたいです。

実現したいこと

フォーム機能でのカテゴリの書き方を習っておらず、自力で調べてもカテゴリの書き方が出てこないので教えて欲しいです。

発生している問題・エラーメッセージ

Class 'App\Http\Controllers\Categories' not found

該当のソースコード

ルーティング

web.php

1<?php 2 3/* 4|-------------------------------------------------------------------------- 5| Web Routes 6|-------------------------------------------------------------------------- 7| 8| Here is where you can register web routes for your application. These 9| routes are loaded by the RouteServiceProvider within a group which 10| contains the "web" middleware group. Now create something great! 11| 12*/ 13Auth::routes(); 14// トップページをログイン画面後に表示させたい場合はmiddlewareを書かないとエラーが出る 15Route::get('/', function () { 16 return view('layouts.top'); 17})->middleware('auth'); 18// お気に入り一覧 19Route::resource('likes', 'LikesController')->only([ 20 'index', 'store', 'destroy' 21]); 22 23Route::resource('items', 'ItemsController'); 24 25Route::resource('profile', 'ProfileController')->only([ 26 'store', 'destroy' 27]); 28Route::get('users/{id}/index', 'ItemsController@index')->name('users.index'); 29 30 31Route::get('/items/{id}/edit_image', 'ItemsController@editImage')->name('items.edit_image'); 32 33Route::patch('/items/{id}/edit_image', 'ItemsController@editImage')->name('items.update_image'); 34 35Route::get('/profile/{id}/edit', 'ProfileController@edit')->name('profile.edit'); 36Route::patch('/profile/{id}', 'ProfileController@update')->name('profile.update'); 37Route::get('/profile/{id}/edit_image', 'ProfileController@editImage')->name('profile.edit_image'); 38Route::patch('/profile/{id}/edit_image', 'ProfileController@updateImage')->name('profile.update_image'); 39 40Route::resource('profile', 'ProfileController')->only([ 41 'show', 42]); 43

コントローラー

ItemsController.php

1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Items; 7use App\User; 8use App\Http\Requests\ItemsRequest; 9use App\Http\Requests\ItemsImageRequest; 10//use App\Http\Requests\ProfileImageRequest; 11use App\Services\FileUploadService; 12 13class ItemsController extends Controller 14{ 15 public function __construct() 16 { 17 $this->middleware('auth'); 18 } 19 20 public function index() 21 { 22 $items = Items::where('user_id', \Auth::user()->id)->get(); 23 return view('users.index',[ 24 'title' => '出品商品一覧', 25 'items' => $items, 26 ]); 27 } 28 29 //新規出品 30 public function create() 31 { 32 return view('items.create',[ 33 'title' => '商品を出品' 34 ]); 35 } 36 37 public function store(ItemsRequest $request, FileUploadService $service) 38 { 39 // 画像投稿処理 40 $path = $this->saveImage($request->file('image')); 41 $path= ''; 42 $image = $request->file('image'); 43 if(isset($image) === true ){ 44 // publicディスク(storage/app/public/)のphotosディレクトリに保存 45 $path = $image->store('photos', 'public'); 46 } 47 48 Items::create([ 49 'user' => \Auth::user(), 50 'categories' => Categories::all(), 51 'description' => $request->description, 52 'image' => $path, //ファイルパスを保存 53 ]); 54 session()->flash('success', '商品を出品しました'); 55 return redirect()->route('users.exhibitions'); 56 } 57 58 // 購入詳細 59 public function show($id) 60 { 61 // 62 } 63 64 // 商品情報の編集 65 public function edit(Items $items) 66 { 67 return view('items.edit',[ 68 'title' => '出品編集', 69 'items' => $items, 70 ]); 71 } 72 73 public function update(Request $request, $id) 74 { 75 $items = Items::find($id); 76 $items->update($request->only(['description'])); 77 session()->flash('success', '出品しました'); 78 return redirect()->route('items.index'); 79 } 80 81 public function destroy($id) 82 { 83 $items = Items::find($id); 84 $items ->delete(); 85 \Session::flash('success', '商品を削除しました'); 86 return redirect()->route('items.index'); 87 } 88 89 public function editImage($id) 90 { 91 $items = Items::find($id); 92 return view('items.edit_image',[ 93 'title' => '画像変更処理', 94 'items' => $items, 95 ]); 96 } 97 98 public function updateImage($id, ItemsImageRequest $request, FileUploadService $service) 99 { 100 101 //画像投稿処理 102 $path = ''; 103 $image = $request->file('image'); 104 105 if(isset($image) === true){ 106 // publicディスク(storage/app/)のphotosディレクトリに保存 107 $path = $image->store('photos', 'public'); 108 } 109 110 $items = Items::find($id); 111 112 // 変更前の画像の削除 113 if($items->image !== ''){ 114 // publicディスクから、該当の投稿画像($user->image)を削除 115 \Storage::disk('public')->delete(\Storage::url($items->image)); 116 } 117 118 $items->update([ 119 'image' => $path, //ファイル名を保存 120 ]); 121 122 session()->flash('success', '画像を変更しました!'); 123 return redirect()->route('users.index', $id); 124 } 125 126 private function saveImage($image){ 127 // 画像投稿処理 128 $path = ''; 129 if(isset($image) === true){ 130 // publicディスク(storage/app/)のphotosディレクトリに保存 131 $path = $image->store('photos', 'public'); 132 } 133 return $path;; // 画像が存在しない場合は空文字 134 } 135 136} 137

ビュー

create.blade.php(新規出品のビュー)

1@extends('layouts.top') 2 3@section('title', $title) 4 5@section('content') 6 <h1>{{ $title }}</h1> 7 <form method="POST" 8 action="{{ action('ItemsController@store') }}" 9 enctype="multipart/form-data"> 10 @csrf 11 <div> 12 <label> 13 商品名: 14 <input type="text" name="name"> 15 </label> 16 </div> 17 <div> 18 <label> 19 商品説明: 20 <input type="text" name="description"> 21 </label> 22 </div> 23 <div> 24 <label> 25 価格: 26 <input type="text" name="price"> 27 </label> 28 </div> 29 <div> 30 <label> 31 カテゴリー: 32 <input type="text" name="categories"> 33 </label> 34 </div> 35 <div> 36 <label> 37 画像を選択: 38 <input type="file" name="image"> 39 </label> 40 </div> 41 <input type="submit" value="出品"> 42 </form> 43@endsection

index.blade.php(出品一覧のビュー)

1@extends('layouts.top') 2 3@section('title', $title) 4 5@section('content') 6 <h1>{{ $title }}</h1> 7 <a href="{{route('items.create')}}">新規出品</a> 8 <ul> 9 @forelse($items as $item) 10 <li class="items"> 11 <div class="items_content"> 12 <div class="items_body_heading"> 13 <div class="items_body_main_img"> 14 @if($items->image !== '') 15 <img src="{{ asset('storage/' . $items->image) }}"> 16 @else 17 <img src="{{ asset('images/no_image.png') }}"> 18 @endif 19 </div> 20 <div class="items_body_main_comment"> 21 {{ $items->description }} 22 </div> 23 </div> 24 <div class="items_body_main"> 25 商品名:{{ $items->name }} 26 {{ $items->price }} 27 </div> 28 <div class="items_category"> 29 カテゴリ:{{ $items->category_id }} 30 ({{ $items->created_at }}) 31 </div> 32 <div class="items_body_footer"> 33 [<a href="{{ route('items.edit', $items) }}">編集</a>] 34 <form class="delete" method="post" action="{{ route('items.destroy', $items) }}"> 35 @csrf 36 @method('DELETE') 37 <input type="submit" value="削除"> 38 </div> 39 </div> 40 </li> 41 @empty 42 <li>出品している商品はありません。</li> 43 @endforelse 44 </ul> 45@endsection

試したこと

カテゴリ、検索機能追加の作成方法の検索

補足情報(FW/ツールのバージョンなど)

Windows10 cloud9 Laravel6_v1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問