Laravel5.5
MySQL
Twitterのように、投稿に対して「いいね!」機能のあるサイトを考案中です。
そのため、データベースで問い合わせたCOUNT(*)の数をViewに表示させたいです。
(SQL) SELECT*, COUNT(*) FROM goodpost_favorite GROUP BY favorite_id;
結果
+----+---------+-------------+---------------------+---------------------+----------+
| id | user_id | favorite_id | created_at | updated_at | COUNT(*) |
+----+---------+-------------+---------------------+---------------------+----------+
| 3 | 2 | 1 | 2018-08-25 15:48:58 | 2018-08-25 15:48:58 | 4 |
| 4 | 2 | 2 | 2018-08-25 15:49:13 | 2018-08-25 15:49:13 | 3 |
| 5 | 2 | 3 | 2018-08-25 15:49:24 | 2018-08-25 15:49:24 | 3 |
| 10 | 3 | 4 | 2018-08-25 15:51:18 | 2018-08-25 15:51:18 | 2 |
| 9 | 3 | 5 | 2018-08-25 15:51:17 | 2018-08-25 15:51:17 | 3 |
| 17 | 4 | 6 | 2018-08-25 15:53:07 | 2018-08-25 15:53:07 | 2 |
| 16 | 4 | 7 | 2018-08-25 15:53:06 | 2018-08-25 15:53:06 | 2 |
| 18 | 4 | 8 | 2018-08-25 15:54:50 | 2018-08-25 15:54:50 | 1 |
+----+---------+-------------+---------------------+---------------------+----------+
これの、 COUNT(*) のみを取得して、投稿に「いいね!」を表示させたいです。
goodpost_favoriteテーブルの、favorite_idの中にgoodpostのidが入っています。
現状、Viewはこのような表示で、きれいに表示されてません。
Aさん
ああああ。
[4いいね!]
Aさん
ああああ。
[3いいね!]
Aさん
ああああ。
[3いいね!]
Aさん
ああああ。
[1いいね!]
Aさん
かきくけです。
[4いいね!]
Aさん
かきくけです。
[3いいね!]
Aさん
かきくけです。
[3いいね!]
Aさん
かきくけです。
[1いいね!]
Bさん
XYZ・・・。
[4いいね!]
Bさん
XYZ・・・。
[3いいね!]
Bさん
XYZ・・・。
[3いいね!]
Bさん
XYZ・・・。
[1いいね!]
このように、投稿数×投稿数の大量の表示になってしまい、投稿に紐づいた「いいね!」を投稿に表示させられません。
たぶん、配列で渡されたものをうまく抽出できていないせいだと思うのですが、そのやり方がわかりません。
コントローラ側、もしくはブレード側でどのようにコードを組めばうまく表示されますでしょうか、ご教授いただきたく投稿しました。
よろしくおねがいします。
goodposts.blade.php <ul class="media-list"> @foreach ($goodposts as $goodpost) @foreach ($favorite_counts as $favorite_count) <?php $user = $goodpost->user; ?> <li class="media"> <div class="media-body"> <div> {!! link_to_route('users.show', $user->name, ['id' => $user->id]) !!} <span class="text-muted">posted at {{ $goodpost->created_at }}</span> </div> <div> <p>{!! nl2br(e($goodpost->content)) !!}</p>{{ $favorite_count->count}} いいね! </div> <div> @include('goodpost_favorite.favorite_button', ['goodpost' => $goodpost]) </div> </div> </li> @endforeach @endforeach </ul> {!! $goodposts->render() !!}
GoodpostsController.php @index public function index() { $data = []; if (\Auth::check()) { $user = \Auth::user(); $goodposts = $user->feed_goodposts()->orderBy('created_at', 'desc')->paginate(10); $favorite_counts = \DB::table('goodpost_favorite') ->select(\DB::raw('favorite_id, count(*) as count')) ->groupBy('favorite_id') ->get(); $data = [ 'user' => $user, 'goodposts' => $goodposts, ]; } return view('welcome', $data, [ 'favorite_counts' => $favorite_counts, ]); }
goodpost_favorite_table.php <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateGoodpostFavoriteTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('goodpost_favorite', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->integer('favorite_id')->unsigned()->index(); $table->timestamps(); // 外部キー設定 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('favorite_id')->references('id')->on('goodposts')->onDelete('cascade'); // user_idとfavorite_idの組み合わせの重複を許さない $table->unique(['user_id', 'favorite_id']); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('goodpost_favorite'); } }
⇩思い描く表示です。
Aさん
ああああ。
[4いいね!]
Aさん
かきくけです。
[3いいね!]
Bさん
XYZ・・・。
[3いいね!]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/31 09:14