前提・実現したいこと
ラーメン屋投稿アプリを作っおり、画像投稿機能で詰まっています。お店投稿画面'/shop/new'からsubmitすると、エラーがでてしまいますl。おそらくShopController.phpのstoreメソッド内の$fileがnullなためだと思うのですが、これ以上どう対処すればパスがつながるのかわかりません。また、コードにもいまいち自信がありませんので、可能であればご指摘いただけると光栄です。
発生している問題・エラーメッセージ
Call to a member function getClientOriginalExtension() on null
該当のソースコード
/Applications/MAMP/htdocs/ramenmap/app/Http/Controllers/ShopController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Shop; 6use App\Category; 7use App\SubCategory; 8use App\Pic; 9use Illuminate\Http\Request; 10use Intervention\Image\ImageManagerStatic as Image; 11 12class ShopController extends Controller 13{ 14 public function __construct() 15 { 16 $this->middleware('auth')->except(['index', 'show']); 17 } 18 /** 19 * Display a listing of the resource. 20 * 21 * @return \Illuminate\Http\Response 22 */ 23 public function index() 24 { 25 $shops = Shop::latest()->get(); 26 return view('index', ['shops' => $shops]); 27 } 28 29 /** 30 * Show the form for creating a new resource. 31 * 32 * @return \Illuminate\Http\Response 33 */ 34 public function create(Request $request) 35 { 36 $shop = new Shop(); 37 $shop->name = $request->name; 38 $categories = Category::all()->pluck('name', 'id'); 39 $subcategories = SubCategory::all()->pluck('name', 'id'); 40 $image = Pic::all(); 41 return view('new', ['categories' => $categories, 'subcategories' => $subcategories, 'image' => $image]); 42 } 43 44 /** 45 * Store a newly created resource in storage. 46 * 47 * @param \Illuminate\Http\Request $request 48 * @return \Illuminate\Http\Response 49 */ 50 public function store(Request $request) 51 { 52 $shop = new Shop; 53 $user = \Auth::user(); 54 55 $shop->name = request('name'); 56 $shop->address= request('address'); 57 $shop->category_id = request('category_id'); 58 $shop->subcategory_id = request('subcategory_id'); 59 $shop->user_id = $user->id; 60 $shop->image = request('image'); 61 $shop->save(); 62// dd($shop); 63 // $request->validate([ 64 // 'image'=>'required|image|mimes:jpg,jpeg,png|max:2000' 65 // ]); 66 $file = $request->file('image'); 67// dd($file); 68 $fileName = str_random(20).'.'.$file->getClientOriginalExtension(); 69// dd($filename); 70 Image::make($file)->save(public_path('images/'.$fileName)); 71 $pic=new Pic; 72 $pic->image = $fileName; 73 $pic->save(); 74 75 return redirect()->route('shop.detail', ['id' => $shop->id]); 76 } 77 78 /** 79 * Display the specified resource. 80 * 81 * @param \App\Shop $shop 82 * @return \Illuminate\Http\Response 83 */ 84 public function show($id) 85 { 86 $shop = Shop::find($id); 87 $user = \Auth::user(); 88 if ($user) { 89 $login_user_id = $user->id; 90 } else { 91 $login_user_id = ''; 92 } 93 94 return view('show', ['shop' => $shop, 'login_user_id'=>$login_user_id]); 95 } 96 97 /** 98 * Show the form for editing the specified resource. 99 * 100 * @param \App\Shop $shop 101 * @return \Illuminate\Http\Response 102 */ 103 public function edit(Shop $shop, $id) 104 { 105 $shop = Shop::find($id); 106 $categories = Category::all()->pluck('name', 'id'); 107 $subcategories = SubCategory::all()->pluck('name', 'id'); 108 return view('edit', ['shop' => $shop, 'categories' => $categories, 'subcategories' => $subcategories]); 109 } 110 111 /** 112 * Update the specified resource in storage. 113 * 114 * @param \Illuminate\Http\Request $request 115 * @param \App\Shop $shop 116 * @return \Illuminate\Http\Response 117 */ 118 public function update(Request $request, Shop $shop, $id) 119 { 120 $shop = Shop::find($id); 121 $shop->name = request('name'); 122 $shop->address = request('address'); 123 $shop->category_id = request('category_id'); 124 $shop->subcategory_id = request('subcategory_id'); 125 $shop->save(); 126 return redirect()->route('shop.detail', ['id' => $shop->id]); 127 } 128 129 /** 130 * Remove the specified resource from storage. 131 * 132 * @param \App\Shop $shop 133 * @return \Illuminate\Http\Response 134 */ 135 public function destroy($id) 136 { 137 $shop = Shop::find($id); 138 $shop = destroy(); 139 return redirect('/shops'); 140 } 141} 142
/Applications/MAMP/htdocs/ramenmap/resources/views/new.blade.php
@extends('layout') @section('content') <h3>お店投稿</h3><br> {{ Form::open(['method' =>'post', 'url' => '/shop', 'files' => true]) }} <div class="form-group"> {{ Form::label('name', '店名') }} {{ Form::text('name', null) }} </div> <div class="form-group"> {{ Form::label('address', '住所') }} {{ Form::text('address', null) }} </div> <div class="form-group"> {{ Form::label('category_id', 'カテゴリ') }} {{ Form::select('category_id', $categories, '選択してくだい') }} </div> <div class="form-group"> {{ Form::label('subcategory_id','サブカテゴリ') }} {{ Form::select('subcategory_id', $subcategories, '選択してください') }} </div> <div class="form-group"> {{ Form::file('image', null) }} </div> <div class="form-submit"> <button type="submit">投稿する</button> </div> </div> </form> {{ Form::close() }} <div> <a href="{{ route('shop.list') }}">一覧に戻る</a> </div> @endsection
補足情報(FW/ツールのバージョンなど)
PHP 7.1.23
Laravel Framework 5.7.28
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/27 07:35
退会済みユーザー
2019/08/27 07:37 編集
2019/08/27 07:51
2019/08/27 08:26
2019/08/27 08:27
2019/08/27 10:09