お世話になってます!
題名の通り、例えばセレクトボックスを作ってクリック時に指定したソート順にページを推移したいです!
連想配列を作ったりしているのですがうまくいかないので質問させていただきます。
現在はbook.showのページで紐づく投稿のデータを置換しようとしています。
book.controler public function show(Book $book, Sentence $sentence, Favorite $favorite) { $user = auth()->user(); $book = $book->getBook($book->id); $request = ["最新", "人気"]; switch($request) { case "最新": $sentences = $sentence->getSentence($book->id); break; case "人気": $sentences = $sentence->sentenceWithCount($book->id); break; default: $sentences = $sentence->getSentence($book->id); } $favorite = $favorite->all(); return view('books.show', compact('book'),[ 'user' => $user, 'book' => $book, 'sentences' => $sentences, 'request' => $request, 'favorite' => $favorite, ]); }
viewです <div class="book-show-below"> <div class="sentence-box"> <select class="select" name="select" onChange="location.href=value;"> @foreach ($request as $sentence_text) <option value="{{ url()->current() }}?sentence={{ $sentence_text }}"> {{ $sentence_text }} </option> @endforeach </select> @foreach($sentences as $sentence) @if(isset($user)) @if (!in_array(Auth::user()->id, array_column($sentence->favorites->toArray(), 'user_id'), TRUE)) <form action="{{ route('favorites', $sentence) }}" method="POST" class="favorite-like"> @csrf <input type="hidden" name="sentence_id" value="{{ $sentence->id }}"> <input type="submit" value="いいね" class="fas btn btn-like"> </form> @else <form action="{{ route('unfavorites', $sentence) }}" method="POST" class="favorite-unlike"> @csrf <input type="hidden" name="sentence_id" value="{{ $sentence->id }}"> <input type="submit" value="いいね取り消す" class="fas btn btn-unlike"> </form> @endif @endif <div class="sentence-box-each"> <a href="{{ route('users.show',$sentence->user->id)}}"></a> <div class="sentence-box-name"> <div class="sentence-box-name-each"> {{ $sentence->user->name}}さんのまとめ</div> <div class="line"></div> <div class="sentence-box-text-each"> <p>{{ $sentence->text }}</p> </div> </div> <div class="sentence-box-user"> <img class="profile_image" src="{{ asset('storage/profile_image/' .$sentence->user->profile_image)}}"> </div> </div> @endforeach </div> </div> </body>
修正しました!
switch文にしてるのですが、現状リストが機能していません
回答1件
あなたの回答
tips
プレビュー