前提
Laravelにてフリーマーケットサイトを作成しています。
そこで出品商品一覧画面や新規商品を作るビューなどは作ることに成功しました。
実現したいこと
タイトルに書いたように、商品を買いたい場合の購入処理の書き方が自力で調べても分からないので教えてほしいです。
購入用の処理やボタンは商品詳細ビューのshow.bladeに実装したいです。
該当のソースコード
ルーティング
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/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\Item; 7use App\User; 8use App\category; 9use App\Http\Requests\ItemsRequest; 10use App\Http\Requests\ItemsImageRequest; 11//use App\Http\Requests\ProfileImageRequest; 12use App\Services\FileUploadService; 13 14class ItemsController extends Controller 15{ 16 public function __construct() 17 { 18 $this->middleware('auth'); 19 } 20 21 public function index() 22 { 23 $items = Item::where('user_id', \Auth::user()->id)->get(); 24 return view('users.index',[ 25 'title' => '出品商品一覧', 26 'items' => $items, 27 ]); 28 } 29 30 //新規出品 31 public function create() 32 { 33 return view('items.create',[ 34 'title' => '商品を出品' 35 ]); 36 } 37 //store の中に requestを書く事でリクエストフォームからデータを引っ張ってくる 38 public function store(ItemsRequest $request, FileUploadService $service) 39 { 40 // 画像投稿処理 41 $path = $this->saveImage($request->file('image')); 42 $path= ''; 43 $image = $request->file('image'); 44 if(isset($image) === true ){ 45 // publicディスク(storage/app/public/)のphotosディレクトリに保存 46 $path = $image->store('photos', 'public'); 47 } 48 // テーブルに存在する左側の項目に何を入れるか→リクエストフォーム($request)からname,descriptionなど 49 Item::create([ 50 'user_id' => \Auth::user()->id, 51 'name' => $request->name, 52 'description' => $request->description, 53 'category_id' => $request->category_id, 54 'price' => $request->price, 55 'image' => $path, //ファイルパスを保存 56 ]); 57 session()->flash('success', '商品を出品しました'); 58 return redirect()->route('users.index'); 59 } 60 61 // 購入詳細 62 public function show($id) 63 { 64 // 65 } 66 67 // 商品情報の編集 68 public function edit(Items $items) 69 { 70 return view('items.edit',[ 71 'title' => '出品編集', 72 'items' => $items, 73 ]); 74 } 75 76 public function update(Request $request, $id) 77 { 78 $items = Item::find($id); 79 $items->update($request->only(['description'])); 80 session()->flash('success', '出品しました'); 81 return redirect()->route('items.index'); 82 } 83 84 public function destroy($id) 85 { 86 $items = Item::find($id); 87 $items ->delete(); 88 \Session::flash('success', '商品を削除しました'); 89 return redirect()->route('items.index'); 90 } 91 92 public function editImage($id) 93 { 94 $items = Item::find($id); 95 return view('items.edit_image',[ 96 'title' => '画像変更処理', 97 'items' => $items, 98 ]); 99 } 100 101 public function updateImage($id, ItemsImageRequest $request, FileUploadService $service) 102 { 103 104 //画像投稿処理 105 $path = ''; 106 $image = $request->file('image'); 107 108 if(isset($image) === true){ 109 // publicディスク(storage/app/)のphotosディレクトリに保存 110 $path = $image->store('photos', 'public'); 111 } 112 113 $items = Item::find($id); 114 115 // 変更前の画像の削除 116 if($items->image !== ''){ 117 // publicディスクから、該当の投稿画像($user->image)を削除 118 \Storage::disk('public')->delete(\Storage::url($items->image)); 119 } 120 121 $items->update([ 122 'image' => $path, //ファイル名を保存 123 ]); 124 125 session()->flash('success', '画像を変更しました!'); 126 return redirect()->route('users.index', $id); 127 } 128 129 private function saveImage($image){ 130 // 画像投稿処理 131 $path = ''; 132 if(isset($image) === true){ 133 // publicディスク(storage/app/)のphotosディレクトリに保存 134 $path = $image->store('photos', 'public'); 135 } 136 return $path;; // 画像が存在しない場合は空文字 137 } 138 139} 140
ビュー
show.blade.php(商品詳細ビュー)
1@extends('layouts.top') 2 3@section('title', $title) 4 5@section('content') 6 <h1>{{ $title }}</h1> 7@endsection
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
Windows10 Laravel6_v1 cloud9
あなたの回答
tips
プレビュー