前提・実現したいこと
初学者です。laravelでラーメン屋投稿アプリを作っています。お店の投稿画面作成中に、$subcategoriesの変数定義の有無で詰まってしまいました。$categoriesは問題ありません。色々ググりましたがこの先有益な情報が見つけられそうにないので、投稿させていただきました。どうかよろしくお願いいたします。
発生している問題・エラーメッセージ
Undefined variable: subcategories (View: /Applications/MAMP/htdocs/ramenmap/resources/views/new.blade.php)
該当のソースコード
php
1### new.blade.php ### 2 3@extends('layout') 4 5@section('content') 6 <h2>お店投稿</h2> 7 {{ Form::open(['route' => 'shop.store']) }} 8 <div class="form-group"> 9 {{ Form::label('name', '店名') }} 10 {{ Form::text('name', null) }} 11 </div> 12 <div class="form-group"> 13 {{ Form::label('address', '住所') }} 14 {{ Form::text('address', null) }} 15 </div> 16 <div class="form-group"> 17 {{ Form::label('category', 'カテゴリ') }} 18 {{ Form::select('category_id', $categories) }} 19 </div> 20 <div class="form-group"> 21 {{ Form::label('subcategories','サブカテゴリ') }} 22 23 {{ Form::select('subcategory_id', $subcategories) }} 24 </div> 25 26 27 <div class="form-submit"> 28 <button type="submit">投稿する</button> 29 </div> 30 </div> 31 </form> 32 {{ Form::close() }} 33 <div> 34 <a href="{{ route('shop.list') }}">一覧に戻る</a> 35 </div> 36@endsection 37
php
1### SubCategory.php ### 2 3<?php 4 5namespace App; 6 7use Illuminate\Database\Eloquent\Model; 8 9class SubCategory extends Model 10{ 11 protected $table = 'subcategories'; 12} 13
php
1### ShopController.php ### 2 3<?php 4namespace App\Http\Controllers; 5 6use App\Shop; 7use App\Category; 8use APP\SubCategory; 9use Illuminate\Http\Request; 10 11class ShopController extends Controller 12{ 13 /** 14 * Display a listing of the resource. 15 * 16 * @return \Illuminate\Http\Response 17 */ 18 public function index() 19 { 20 $shops = Shop::all(); 21 return view('index', ['shops' => $shops]); 22 } 23 24 /** 25 * Show the form for creating a new resource. 26 * 27 * @return \Illuminate\Http\Response 28 */ 29 public function create() 30 { 31 $categories = Category::all()->pluck('name', 'id'); 32 return view('new', ['categories' => $categories]); 33 34 $subcategories = SubCategory::all()->pluck('name', 'id'); 35 return view('new', ['subcategories' => $subcategories]); 36 } 37 38 /** 39 * Store a newly created resource in storage. 40 * 41 * @param \Illuminate\Http\Request $request 42 * @return \Illuminate\Http\Response 43 */ 44 public function store(Request $request) 45 { 46 // 47 } 48 49 /** 50 * Display the specified resource. 51 * 52 * @param \App\Shop $shop 53 * @return \Illuminate\Http\Response 54 */ 55 public function show($id) 56 { 57 $shop = Shop::find($id); 58 return view('show', ['shop' => $shop]); 59 } 60 61 /** 62 * Show the form for editing the specified resource. 63 * 64 * @param \App\Shop $shop 65 * @return \Illuminate\Http\Response 66 */ 67 public function edit(Shop $shop) 68 { 69 // 70 } 71 72 /** 73 * Update the specified resource in storage. 74 * 75 * @param \Illuminate\Http\Request $request 76 * @param \App\Shop $shop 77 * @return \Illuminate\Http\Response 78 */ 79 public function update(Request $request, Shop $shop) 80 { 81 // 82 } 83 84 /** 85 * Remove the specified resource from storage. 86 * 87 * @param \App\Shop $shop 88 * @return \Illuminate\Http\Response 89 */ 90 public function destroy(Shop $shop) 91 { 92 // 93 } 94} 95
php
1### Web.php ### 2<?php 3 4Route::get('/shops', 'ShopController@index')->name('shop.list'); 5Route::get('/shop/new', 'ShopController@create')->name('shop.new'); 6Route::post('/shop', 'ShopController@store')->name('shop.store'); 7Route::get('/shop/{id}', 'ShopController@show')->name('shop.detail'); 8 9Route::get('/', function() { 10 return redirect('/shops'); 11}); 12
補足情報(FW/ツールのバージョンなど)
PHP 7.1.23
Laravel Framework 5.7.28
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/17 04:37
2019/08/17 05:35