初心者で恐縮なのですが、質問させてください。
Laravel、EloquentのorderByを使ってデータベースから取得した情報をソートしたいと思っています。
記事(mutters)と、そのレビュー(revues)のテーブルがあると仮定します。
mutters
========
id
title
text
========
revues
========
id
star(レビューの星の数)
mutter_id
========
muttersはいくつものレビューを持っています(hasmanyの関係)。
そのレビューの平均でソートしたいと考えています。
Laravel
1//やりたいこと 2$mutters = Mutter::orderBy('revues->starの平均値', 'desc')->paginate(20);
分かる方がいましたらご教授いただければと思います。
またデータベースから見直した方が良いなどの意見がありましたらお願いいたします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

回答2件
0
こちら解決したと思ったのですが、get()してしまうとページネーションできないっぽいです。
投稿2018/08/17 03:55
編集2018/08/21 13:57総合スコア8
0
以下、サンプルです。
取得したデータベースコレクションをsortBy
を使って並び替えています。
Controller(抜粋)
php
1 /** 2 * Display a listing of the resource. 3 * 4 * @return \Illuminate\Http\Response 5 */ 6 public function index() 7 { 8 $matterCollections = Matter::with('reviews')->get(); 9 $matters = $matterCollections->sortByDesc(function($matter){ 10 return $matter->reviews->avg('stars'); 11 }); 12 return view('test')->with('matters',$matters); 13 }
テスト用ビュー
@extends('layouts.app') @section('content') <div class="container"> <table class="table table-striped table-bordered table-hover"> <tr> <th>ID</th> <th>タイトル</th> <th>本文</th> <th>スターの平均</th> </tr> @foreach ($matters as $matter) <tr> <td>{{$matter->id}}</td> <td>{{$matter->title}}</td> <td>{{$matter->text}}</td> <td>{{$matter->reviews->avg('stars')}}</td> </tr> @endforeach </table> </div> @endsection
データベース
matters(記事)テーブル
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateMattersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('matters', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->string('title'); 19 $table->string('text',2000); 20 $table->timestamps(); 21 }); 22 } 23 24 /** 25 * Reverse the migrations. 26 * 27 * @return void 28 */ 29 public function down() 30 { 31 Schema::dropIfExists('matters'); 32 } 33}
レビューテーブル
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateReviewsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('reviews', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->boolean('stars'); 19 $table->integer('matter_id'); 20 $table->timestamps(); 21 }); 22 } 23 24 /** 25 * Reverse the migrations. 26 * 27 * @return void 28 */ 29 public function down() 30 { 31 Schema::dropIfExists('reviews'); 32 } 33}
Controller(抜粋)
php
1 /** 2 * Display a listing of the resource. 3 * 4 * @return \Illuminate\Http\Response 5 */ 6 public function index() 7 { 8 $matters = Matter::with('reviews')->get(); 9 $value = $matters->sortByDesc(function($matter){ 10 return $matter->reviews->avg('stars'); 11 }); 12 return view('test')->with('matters',$value); 13 }
投稿2018/08/17 02:18
総合スコア3027
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。