前提・実現したいこと
PHP/Laravelで簡単なお店投稿アプリを学習用・ポートフォリオ用に作っています。herokuへのデプロイはできましたが、画像表示ができないので投稿させていただきました。
Amazon S3に画像をアップロードして表示させようとしていますが、ネット上には多種多様のコードがあり、いくつかお手本にしましたがうまくパスが通せません。
効率悪くもこの3日間で20時間ほどは試行錯誤しており、おそらく原因は僕のPHPの知見不足でそれぞれのコードへの理解不足で応用がきかないためですが。。
よろしくお願いいたします。
発生している問題・エラーメッセージ
画像が表示されません。以下、index.blade.phpの画像のchrome検証表示です。 <img src="http://ramenmap.herokuapp.com/storage/image/Yeenj2cCba5t4I3Az6MVQpE5PUWcWnwsmqMNHAWO.jpeg" class="card-img">
該当のスコード
ShopController.php
<?php namespace App\Http\Controllers; use App\Shop; use App\Category; use App\SubCategory; use Illuminate\Http\Request; use App\Http\Requests\CreateShop; use Illuminate\Support\Facades\Storage; class ShopController extends Controller { public function __construct() { $this->middleware('auth')->except(['index', 'show']); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $q = \Request::query(); if (isset($q['category_id'])) { $shops = Shop::latest()->where('category_id', $q['category_id'])->paginate(5); $shops->load('category', 'user'); return view('index', ['shops' => $shops]); } elseif (isset($q['subcategory_id'])) { $shops = Shop::latest()->where('subcategory_id', $q['subcategory_id'])->paginate(5); $shops->load('category','subcategory', 'user'); return view('index', ['shops' => $shops]); } else { $shops = Shop::latest()->paginate(5); $shops->load('category', 'subcategory', 'user'); return view('index', ['shops' => $shops]); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { $shop = new Shop(); $shop->name = $request->name; $categories = Category::all()->pluck('name', 'id'); $subcategories = SubCategory::all()->pluck('name', 'id'); return view('new', [ 'categories' => $categories, 'subcategories' => $subcategories, ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(CreateShop $request) { $shop = new Shop; $user = \Auth::user(); $shop->name = request('name'); $shop->address = request('address'); $shop->category_id = request('category_id'); $shop->subcategory_id = request('subcategory_id'); $shop->user_id = $user->id; $uploadImg = $shop->image = $request->file('image'); $path = Storage::disk('s3')->putFile('/', $uploadImg, 'public'); $shop->image = Storage::disk('s3')->url($path); $shop->review = request('review'); $shop->save(); return redirect()->route('shop.detail', ['id' => $shop->id]); } /** * Display the specified resource. * * @param \App\Shop $shop * @return \Illuminate\Http\Response */ public function show($id) { $shop = Shop::find($id); $user = \Auth::user(); if ($user) { $login_user_id = $user->id; } else { $login_user_id = ''; } return view('show', ['shop' => $shop, 'login_user_id' => $login_user_id]); } /** * Show the form for editing the specified resource. * * @param \App\Shop $shop * @return \Illuminate\Http\Response */ public function edit(Shop $shop, $id) { $shop = Shop::find($id); $categories = Category::all()->pluck('name', 'id'); $subcategories = SubCategory::all()->pluck('name', 'id'); return view('edit', ['shop' => $shop, 'categories' => $categories, 'subcategories' => $subcategories]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Shop $shop * @return \Illuminate\Http\Response */ public function update(CreateShop $request, Shop $shop, $id) { $shop = Shop::find($id); // $user = \Auth::user(); $shop->name = request('name'); $shop->address = request('address'); $shop->category_id = request('category_id'); $shop->subcategory_id = request('subcategory_id'); // $shop->user_id = $user->id; $filename = $request->file('image')->store('public/image'); $shop->image = basename($filename); $shop->review = request('review'); $shop->save(); return redirect()->route('shop.detail', ['id' => $shop->id]); } /** * Remove the specified resource from storage. * * @param \App\Shop $shop * @return \Illuminate\Http\Response */ public function destroy($id) { $shop = Shop::find($id); $shop->destroy($id); return redirect('/shops'); } }
index.blade.php
@extends('layout') @section('content') @auth <div class="text-right"> <a href={{ route('shop.new') }} class='btn btn-outline-info'>お店投稿</a> </div> @endauth <h4>お店一覧</h4><br> @foreach ($shops as $shop) <div class="card mb-3" style="width: 540;"> <div class="row no-gutters"> <div class="col-md-4"> <img src="{{ $shop->image }}" class="card-img"> </div> <div class="col-md-8"> <div class="card-body"> <h3 class="card-title"> <a href="{{ route('shop.detail', ['id' => $shop->id]) }}">{{ $shop->name }}</a> </h5> <p class="card-text"> <small class='text-muted'>カテゴリ: <a href="{{ route('shop.list', ['category_id' => $shop->category_id])}}"> {{ $shop->category->name }} </a> | <a href="{{ route('shop.list', ['subcategory_id' => $shop->subcategory_id])}}"> {{ $shop->subcategory->name }} </a> </small></p> <p class="card-text">住所: {{ $shop->address }}</p> <p>{{ $shop->review }}</p> <p class="card-text"><small class="text-muted"> by <a href="{{ route('users.show', $shop->user_id) }}">{{ $shop->user->name }}</a> 投稿日時: {{ $shop->created_at }}</small></p> </div> </div> </div> </div> @endforeach {{ $shops->links() }} @endsection
補足情報(FW/ツールのバージョンなど)
PHP 7.1.23
Laravel Framework 5.7.28