laravelでvueを利用し、いいね機能を実装しました。現在、投稿をいいねが多い順にランキング表示させようとしているのですが、うまくいきません。ソースコードは以下になります。
posts:投稿
likes:いいね
/app/Http/Controllers/PostController.php
laravel
1public function index() 2 { 3 $user = \Auth::user(); 4 $id = Auth::id(); 5 6 $posts = Post::orderBy('likes', 'desc')->paginate(20); 7 8 $posts->load('user', 'tags','likes'); 9 return view('top', [ 10 'posts' => $posts, 11 'user' => $user 12 ]); 13 }
/resources/views/top.blade.phpの投稿表示部分
laravel
1@foreach($posts as $post) 2 <div class="col-6 col-sm-4 col-lg-3" style="padding:8px;"> 3 <div class="card"> 4 <a href="{{ route('posts.show', $post->id) }}"> 5 <img class="card-img-top img-responsive center-block" src="{{ asset('storage/image/'.$post->image) }}" alt=""> 6 </a> 7 <div class="card-body text-center"> 8 <h5> 9 <a class="text-secondary" href="{{ route('posts.show', $post->id) }}"> 10 {{ $post->title }} 11 </a> 12 </h5> 13 <h6> 14 <a class="text-secondary" href="{{ route('users.show', $post->user_id) }}"> 15 {{ $post->user->name }} 16 </a> 17 </h6> 18 <h6> 19 @foreach($post->tags as $tag) 20 <a href="{{ route('posts.index', ['tag_name' => $tag->tag_name]) }}"> 21 #{{ $tag->tag_name }} 22 </a> 23 @endforeach 24 </h6> 25 {{ count($post->likes) }} <!--いいねの数表示--> 26 </div> 27 </div> 28 </div> 29@endforeach
View内で{{ count($post->likes) }}を記述することでlikesの数は表示できました。
エラーは表示されないのですが、順番が変わっていない状況です。
他に追記すべきものがあれば指摘していただくと助かります。回答よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー