実現したいこと
画像投稿機能の実装を目指しているのですが、ストレージに保存された画像をトップページに表示されることが出来ません。
ストレージに保存された画像を表示させるコードの書き方が、以下で適切かを見て貰えないでしょうか?
前提
ストレージに画像を保存することは出来ています。
発生している問題・エラーメッセージ
画像が表示されずに四角い写真のマークが出ております。
該当のソースコード
home.blade.php
1@extends('layouts.app') 2 3@section('content') 4 @auth 5 <div class="container"> 6 logged in!</br> 7 <a href="{{ route('post.create') }}">掲載</a> 8 </div> 9 @foreach($posts as $post) 10 <div class='card'> 11 <a href="/posts/{{$post['id']}}"> 12 <img src='{{ Storage::url($post->image)}}' width="100px"></br> 13 @if($post->gender == 'オス') 14 {{ $post['name'] }}くん</br> 15 @else 16 {{ $post['name'] }}ちゃん</br> 17 @endif 18 </a> 19 </div> 20 @endforeach 21 @else 22 <div class="container"> 23 home 24 </div> 25 @endauth 26@endsection
HomeController
1<?php 2 3namespace App\Http\Controllers; 4use App\Models\Post; 5use Illuminate\Http\Request; 6use Illuminate\Support\Facades\Auth; 7use Illuminate\Support\Facades\DB; 8 9class HomeController extends Controller 10{ 11 /** 12 * Create a new controller instance. 13 * 14 * @return void 15 */ 16 public function __construct() 17 { 18 $this->middleware('auth'); 19 } 20 21 /** 22 * Show the application dashboard. 23 * 24 * @return \Illuminate\Contracts\Support\Renderable 25 */ 26 public function index() 27 { 28 $posts = Post::all(); 29 return view('home', ['posts' => $posts]); 30 //return view('home'); 31 } 32}
web.php
1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\PostsController; //投稿機能 5 6/* 7|-------------------------------------------------------------------------- 8| Web Routes 9|-------------------------------------------------------------------------- 10| 11| Here is where you can register web routes for your application. These 12| routes are loaded by the RouteServiceProvider and all of them will 13| be assigned to the "web" middleware group. Make something great! 14| 15*/ 16 17Route::get('/', function () { 18 return view('home'); 19 //return view('welcome'); 20}); 21 22//Sass(デザイン) 23URL::forceScheme('https'); 24 25//トップページ 26//ユーザー 27Auth::routes(); 28//サインアップ・ログイン後の遷移 29Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); 30 31//投稿 32Route::get('/create', [App\Http\Controllers\PostController::class, 'create'])->name('post.create'); 33Route::post('/posts', [App\Http\Controllers\PostController::class, 'store'])->name('post.store'); 34 35//投稿を押した時 36Route::post('/post', [App\Http\Controllers\PostController::class, 'store'])->name('post.store'); 37
PostController
1<?php 2 3namespace App\Http\Controllers; 4use App\Models\Post; 5use Illuminate\Http\Request; 6use Illuminate\Support\Facades\Auth; 7use Illuminate\Support\Facades\Validator; 8 9class PostController extends Controller 10{ 11 public function index() 12 { 13 // データベース内のすべてのpostを取得し、post変数に代入 14 $posts = Post::all(); 15 // 'posts'フォルダ内の'index'viewファイルを返す。 16 // その際にview内で使用する変数を代入します。 17 return view('posts/index', ['posts' => $posts]); 18 } 19 20 public function show($id) 21 { 22 $post = Post::find($id); // idでPostを探し出す 23 return view('posts.show', ['post' => $post]); 24 } 25 26 public function create () 27 { 28 return view('posts/create'); 29 } 30 31 public function new() 32 { 33 return view('post/new'); 34 35 } 36 37 public function destroy($id) 38 { 39 $post = Post::find($id); 40 $post->delete(); 41 return redirect('/posts'); 42 } 43 44 public function store(Request $request) 45 { 46 //ユーザーIDを取得 47 $user_id = Auth::id(); 48 // 新しい Item を作成 49 $post = new Post; 50 // フォームから送られてきたデータをそれぞれ代入 51 $post->user_id = $user_id; 52 $post->name = $request->name; 53 54 //画像投稿の記述 55 $post->image = $request->image->store('images'); 56 57 $post->age = $request->age; 58 $post->gender = $request->gender; 59 $post->explanation = $request->explanation; 60 // データベースに保存 61 $post->save(); 62 return redirect('/'); 63 } 64 65 protected function validator(array $data) 66 { 67 return Validator::make($data, [ 68 'name' => ['required', 'string', 'max:255'], 69 'image' => ['file', 'mimes:gif,png,jpg,webp', 'max:3072'], 70 ]); 71 } 72}
試したこと
画像の表示のさせ方、コードの書き方を調べて、記述したのですが、うまく画像を特定できていないのでしょうか?
補足情報(FW/ツールのバージョンなど)
php: 8.1.21
laravel: 10.18.0
!追記!
コンソールから画像が表示されない原因を探ってみたところ、下記の表示がありました。
4CkuxLJ5p5wzf4VkTosZIIxLOSKmfcIiiIWJJ8Gt.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
画像が格納されているファイルを見つけられなかったといった類のエラーのように見えましたが、合っていますでしょうか?
サーバーのエラーログを確認しました。
[previous exception] [object] (Error(code: 0): Undefined constant \"image\" at /home/ec2-user/environment/mysite-test/storage/framework/views/f4b7cd1ceb841d5254e4ac3758f1773e.php:6) [stacktrace]

回答1件
あなたの回答
tips
プレビュー