前提・実現したいこと
Laravel を用いて画像投稿機能を実装したいです。
storeAs メソッドを用いて form で送信された画像ファイルを storage フォルダに保存し、表示させたいです。
発生している問題・エラーメッセージ
storage フォルダ内にファイルが保存がされずに困っています。
SQLで確認したところ、データベース内にフォルダの url を保存することには成功しているようです。
該当のソースコード
php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\User; 6 7use Illuminate\Http\Request; 8 9use App\Record; 10 11class RecordsController extends Controller 12{ 13 /** 14 * Display a listing of the resource. 15 * 16 * @return \Illuminate\Http\Response 17 */ 18 public function index() 19 { 20 $data = []; 21 if (\Auth::check()) { // 認証済みの場合 22 // 認証済みユーザを取得 23 $user = \Auth::user(); 24 // ユーザの投稿の一覧を作成日時の降順で取得 25 $records = Record::all()->sortByDesc("created_at"); 26 27 $data = [ 28 'user' => $user, 29 'records' => $records, 30 ]; 31 } 32 33 // Welcomeビューでそれらを表示 34 return view('welcome', $data); 35 } 36 37 /** 38 * Show the form for creating a new resource. 39 * 40 * @return \Illuminate\Http\Response 41 */ 42 public function create(Request $request) 43 { 44 $post = new Post(); 45 $post->user_id = Auth::user()->id; 46 $post->title = $request->title; 47 $post->content = $request->content; 48 //写真用 データベースに保存 49 $post->image = $request->image->storeAs('public/post_images', Auth::user()->id.'.jpg'); 50 $post->save(); 51 52 return redirect()->route('posts.detail', [ 53 'id' => $post->id, 54 ]); 55 56 return view('records.post'); 57 58 } 59 60 /** 61 * Store a newly created resource in storage. 62 * 63 * @param \Illuminate\Http\Request $request 64 * @return \Illuminate\Http\Response 65 */ 66 public function store(Request $request) 67 { 68 69 $request->validate([ 70 'title' => 'required|max:20', 71 'time' => 'required', 72 'comment' => 'required|max:225', 73 'image' => 'image|file', 74 ]); 75 76 $request->user()->records()->create([ 77 'title' => $request->title, 78 'time' => $request->time, 79 'comment' => $request->comment, 80 'image' => $request->image, 81 ]); 82 83 84 return back(); 85 } 86 87 /** 88 * Display the specified resource. 89 * 90 * @param int $id 91 * @return \Illuminate\Http\Response 92 */ 93 public function show($id) 94 { 95 // idの値でユーザを検索して取得 96 $user = User::findOrFail($id); 97 98 // 関係するモデルの件数をロード 99 $user->loadRelationshipCounts(); 100 101 // ユーザの投稿一覧を作成日時の降順で取得 102 $records = $user->records()->orderBy('created_at', 'desc')->paginate(10); 103 104 // ユーザ詳細ビューでそれらを表示 105 return view('users.show', [ 106 'user' => $user, 107 'records' => $records, 108 ]); 109 } 110 public function showCreateForm() 111 { 112 return view('records/post'); 113 } 114 115 public function detail(Post $post) 116 { 117 return view('posts/detail', [ 118 'user_id' => $post->user_id, 119 'title' => $post->title, 120 'content' => $post->content, 121 'image' => str_replace('public/', 'storage/', $post->image), 122 ]); 123 } 124 125 126 127}
php
1@extends('layouts.app') 2 3@section('content') 4 5 @if (Auth::check()) 6 {!! Form::open(['route' => 'records.store', 'files' => true]) !!} 7 {{ csrf_field() }} 8 </br> 9 <div class="form-group"> 10 <p>訪れた場所(記入例 : Japan / Tokyo)</p> 11 {!! Form::text('title', old('title'), ['class' => 'form-control', 'rows' => '1']) !!} 12 </br> 13 <p>日付</p> 14 {!! Form::date('time', old('time'), ['class' => 'form-control', 'rows' => '1']) !!} 15 </br> 16 <p>コメント</p> 17 {!! Form::textarea('comment', old('comment'), ['class' => 'form-control', 'rows' => '1']) !!} 18 </br> 19 {!! Form::file('image')!!} 20 </br> 21 </br> 22 {!! Form::submit('Post', ['class' => 'btn btn-primary btn-block']) !!} 23 </div> 24 {!! Form::close() !!} 25 26 @else 27 <div class="center jumbotron"> 28 <div class="text-center"> 29 <h1>TABI RECO</h1> 30 {{-- ユーザ登録ページへのリンク --}} 31 {!! link_to_route('signup.get', 'Sign up now!', [], ['class' => 'btn btn-lg btn-primary']) !!} 32 </div> 33 @endif 34@endsection 35 36
ファイルをアップロードするたびにimageカラムの中の値は増えていくので、保存自体はできていると思いますが、開けようとしてみても、No file found と表示されてしまいます。
sql
1mysql> select image FROM records; 2+----------------+ 3| image | 4+----------------+ 5| /tmp/phpAVp4jJ | 6| /tmp/php1nfasl | 7| /tmp/phpJrIpvP | 8| /tmp/phppRcWnf | 9| /tmp/phpO0T069 | 10| /tmp/phpBqBIvY | 11| /tmp/phpCMsWCW | 12| /tmp/phpfRx9xx | 13| /tmp/phpzG1to4 | 14| /tmp/phpz5WiO9 | 15| /tmp/phpBMfAxU | 16+----------------+ 1711 rows in set (0.00 sec)
補足情報(FW/ツールのバージョンなど)
エラーメッセージなどはでておらず、文章や日付の投稿は問題なく行えます。
初学者且つ初めての質問のため、勉強不足な点も多々あると思いますが、何卒よろしくお願い致します。