laravelで画像の表示ができなくて困っています!
前回で名前を指定して保存できるようになったのですが、次は表示できないです。
やったこと
パスの確認
php artisan storage:linkの打ち込み等、色々
現在確認しているサイト
ブログ見ないで公式見るようにしてます!
https://readouble.com/laravel/7.x/ja/filesystem.html?header=%25E8%25A8%25AD%25E5%25AE%259A
model
class Book extends Model { protected $fillable = [ 'title', 'over_view' ]; public function getUrlAttribute() { return url($this->baseUri, $this->filename()); } public function user() { //userIdに紐づく return $this->belongsTo(User::class); } public function sentences() { return $this->hasMany(Sentence::class); } public function tags() { return $this->belongsToMany(Tag::class); } public function getFollowedTimeLines(Int $user_id, Array $follow_ids) { //自身とフォローしているユーザーを結合する $follow_ids[] = $user_id; return $this->whereIn('user_id', $follow_ids)->orderBy('created_at', 'DESC')->paginate(50); } public function getBook(Int $book_id) { //本id取得 return $this->with('user')->where('id', $book_id)->first(); } public function bookStore(Int $user_id, Array $data, $file_name) { $this->user_id = $user_id; $this->book_image = $file_name; $this->title = $data['title']; $this->over_view = $data['over_view']; $this->save(); return; } public function getEditBook(Int $user_id, Int $book_id) { return $this->where('user_id', $user_id)->where('id', $book_id)->first(); } public function bookUpdate(Int $book_id, Array $data) { $this->id = $book_id; $this->title = $data['title']; $this->over_view = $data['over_view']; $this->update(); return; } public function bookDestroy(Int $user_id, Int $book_id) { return $this->where('user_id',$user_id)->where('id',$book_id)->delete(); } //タグ public function bookTagStore(Array $tag_ids){ //attch foreach($tag_ids as $tag_id) { $this->tags()->attach($tag_id); } } public function bookTagSync(Array $tag_ids){ //syncメソッドは中間テーブルに設置しておくIDの配列を渡す。https://yshrfmru.hatenablog.com/entry/2019/03/24/131219 $this->tags()->sync($tag_ids); } public function getTabInfoList(){ $tab_info_list = [ 'タイムライン' => [ 'param' => '', 'icon_class' => 'fas fa-stream' ], '人気' => [ 'param' => '?mode=popular', 'icon_class' => 'fas fa-fire' ], ]; return $tab_info_list; } }
controller
<?php namespace App\Http\Controllers; use App\Models\Book; use App\Models\Sentence; use App\Models\Tag; use App\Models\User; use Illuminate\Support\Facades\Validator; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class BooksController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request, Book $book, Tag $tags, User $user) { $books = Book::all(); $contents = Storage::get('public/book_image'); var_dump($contents); $popular_tags = $tags->getPopularTags(); $popular_users = $user->getPopularUsers(); $tab_info_list = $book->getTabInfoList(); return view('books.index',compact('books'), [ 'popular_tags' => $popular_tags, 'popular_users' => $popular_users, // 'api' => $api, 'tab_info_list' => $tab_info_list, ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ //ok public function create() { $user = auth()->user(); return view('books.create',[ 'user' => $user, ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request, Book $book, Tag $tag) { $user = auth()->user(); $url = Storage::url('book_image'); $file_name = $request->file('book_image')->getClientOriginalName(); $request->file('book_image')->storeAs('public/book_image',$file_name); $data = $request->all(); $validator = Validator::make($data,[ 'title' => ['string', 'max:30'], 'over_view' => ['string', 'max:20480'], 'book_image' => ['file', 'image', 'mimes:jpeg,png,jpg', 'max:20480'] ]); $validator->validate(); $book->bookStore($user->id, $data, $file_name); //タグ挿入 $tag->tagStore($data["tags"]); //$tagテーブルに挿入した値の名前からidを取得し中間テーブルへ $tag_ids = $tag->getTagIds($data["tags"]); //中間テーブルにidを設置 $book->bookTagSync($tag_ids); $book->save(); return redirect('/books')->with('success', '投稿が完了しました。'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show(Book $book, Sentence $sentence) { $user = auth()->user(); $book = $book->getBook($book->id); // $favorite_row = $favorite->getFavoriteRow($user->id, $sentence->id); $sentences = $sentence->getSentence($book->id); return view('books.show', compact('book'),[ 'user' => $user, 'book' => $book, 'sentences' => $sentences, ]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit(Book $book) { $book_status_texts = $book->getPostbookStatusTexts(); $user = auth()->user(); $books = $book->getEditbook($user->id, $book->id); // if(!isset($books)) { // return redirect('books'); // } // $tags = []; // foreach($book->tags as $tag){ // $tags[] = $tag; // } return view('books.edit', [ 'user' => $user, 'books' => $books, // 'tags'=>$tags, // 'book_status_texts' => $book_status_texts, ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, Book $book, Tag $tag) { $data = $request->all(); $validator = Validator::make($data,[ 'title' => ['string', 'max:30'], 'over_view' => ['string', 'max:20480'], 'book_image' => ['file', 'image', 'mimes:jpeg,png,jpg', 'max:20480'] ]); $validator->validate(); $book->bookUpdate($book->id, $data); return back()->with('success', '編集完了しました'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy(Book $book, Request $request) { $user = auth()->user(); $book->bookDestroy($user->id, $book->id); $redirect = $request->input('redirect'); if ($redirect == "on") { return redirect('/'); } else { return back(); } } }
view index
<h1>一覧画面</h1> <p><a href="{{ route('books.create') }}">新規追加</a></p> @if ($message = Session::get('success')) <p>{{ $message }}</p> @endif <table border="1"> <tr> <th>title</th> <th>詳細</th> <th>編集</th> <th>削除</th> </tr> @foreach ($books as $book) <img src="{{ asset('public/book_image' . $book->book_image) }}" alt="{{ $book->book_image }}" width="100px" class="w-100"> <tr> <td>{{ $book->title }}</td> <th><a href="{{ route('books.show',$book->id)}}">詳細</a></th> <th><a href="{{ route('books.edit',$book->id)}}">編集</a></th> <th> <form action="{{ route('books.destroy', $book->id)}}" method="POST"> @csrf @method('DELETE') <input type="submit" name="" value="削除"> </form> </th> </tr> @endforeach </table>
view show
<h1>詳細画面</h1> <p><a href="{{ route('books.index')}}">一覧画面</a></p> <table border="1"> <tr> <th>id</th> <th>title</th> <th>over_view</th> <th>画像</th> </tr> <tr> <td>{{ $book->id }}</td> <td>{{ $book->title }}</td> <td>{{ $book->over_view }}</td> <img src="/storage{{$book->book_image}}"> <img src="{{ URL::to('public/book_image') }}/{{ $book->book_image }}" alt="{{ $book->book_image }}" /> </tr> </table> <th><a href="{{ route('sentences.create', ['id' => $book->id]) }}">コメント</a></th> <div class="col-xs-8 col-xs-offset-2"> @foreach($sentences as $sentence) <tr> <td>{{ $sentence->text }}</td> </tr> <form method="POST" action="{{ route('favorites.store') }}"> @csrf <input type="hidden" name="sentence_id" value="{{ $sentence->id }}"> <button> </form> @endforeach
画像の保存はバリデーション飛ばしてしまってますが、とりあえず画像表示できるようにしたいです。
どなたかご教授よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。