{"id":4596,"date":"2019-09-06T01:02:13","date_gmt":"2019-09-05T16:02:13","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=4596"},"modified":"2019-09-06T01:02:13","modified_gmt":"2019-09-05T16:02:13","slug":"laravel-6-0-%e3%81%ae%e6%96%b0%e3%81%97%e3%81%84%e3%82%b5%e3%83%96%e3%82%af%e3%82%a8%e3%83%aa%e3%83%bc%e3%82%92%e7%b4%b9%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=4596","title":{"rendered":"Laravel 6.0 \u306e\u65b0\u3057\u3044\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u7d39\u4ecb"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001<a href=\"https:\/\/blog.capilano-fw.com\/?p=4590\">\u524d\u56de<\/a>\u30fb<a href=\"https:\/\/blog.capilano-fw.com\/?p=4576\">\u524d\u3005\u56de<\/a>\u3068<code>Laravel<\/code>\u306e\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3<code>6.0<\/code>\u306e\u65b0\u6a5f\u80fd\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3092\u3057\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u4eca\u56de\u306f\u305d\u306e\uff13\u5f3e\u3067\u3059\u304c\u3001\u65b0\u6a5f\u80fd\u3068\u3044\u3046\u3088\u308a\u306f<strong>\u4ee5\u524d\u304b\u3089\u3042\u3063\u305f\u6a5f\u80fd\u3092\u5f37\u5316\u3057\u305f<\/strong>\u3068\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u306b\u306a\u308a\u307e\u3059\u3002\u305f\u3060\u3001\u3053\u308c\u306b\u3088\u3063\u3066\u79c1\u304c\u9577\u5e74\u5e0c\u671b\u3057\u3066\u3044\u305f\u3053\u3068\u3092\u89e3\u6c7a\u3059\u308b\u904b\u3073\u3068\u306a\u308a\u307e\u3057\u305f\ud83d\ude0a\u2728<\/p>\n<p>\u305d\u3057\u3066\u3001\u305d\u306e\u5185\u5bb9\u306f\u3068\u3044\u3046\u3068\u3001<\/p>\n<p><strong style=\"font-size: 35px;\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u30fc<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3082\u3057\u304b\u3059\u308b\u3068\u300c\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u300d\u306f\u3042\u307e\u308a\u99b4\u67d3\u307f\u304c\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u306e\u3067\u3001\u4eca\u56de\u306f\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3068\u306f\u4f55\u304b\uff1f\u3068\u3044\u3046\u6839\u672c\u7684\u306a\u3068\u3053\u308d\u304b\u3089\u3054\u7d39\u4ecb\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\uff08\u3059\u3067\u306b\u77e5\u3063\u3066\u308b\u65b9\u306f<a href=\"#subquery_usage\">\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u306e\u4f7f\u3044\u65b9<\/a>\u307e\u3067\u8aad\u307f\u98db\u3070\u3057\u3066\u304f\u3060\u3055\u3044\uff09<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3344 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22.png\" alt=\"\" width=\"320\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22.png 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-150x150.png 150w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-300x300.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-100x100.png 100w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 6.0<\/p>\n<h1>\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3068\u306f<\/h1>\n<p>\u307e\u305a\u306f\u300c\u30b5\u30d6\u300d\u3067\u306f\u306a\u3044\u6a19\u6e96\u306e\u30af\u30a8\u30ea\u30fc\u304b\u3089\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\u300c\u30af\u30a8\u30ea\u30fc\u300d<\/strong>\u3068\u306f\u5927\u3056\u3063\u3071\u306b\u8a00\u3046\u3068SQL\u6587\u306e\u3053\u3068\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u3082\u306e\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>SELECT * FROM users ORDER BY id DESC<\/strong><\/pre>\n<p>\u6700\u8fd1\u306e\u958b\u767a\u3067\u306f\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u3046\u3053\u3068\u304c\u591a\u304f\u306a\u3063\u305f\u306e\u3067\u3001SQL\u6587\u3092\u76f4\u63a5\u66f8\u304f\u3053\u3068\u306f\u5c11\u306a\u304f\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u3053\u308c\u304c\u57fa\u672c\u306e<strong>\u300c\u30af\u30a8\u30ea\u30fc\u300d<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001<strong>\u300c\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u300d<\/strong>\u3068\u3044\u3046\u306e\u306f\u3001<strong>\u3082\u3063\u3068\u8a73\u3057\u3044\u6761\u4ef6\u6307\u5b9a\u3092\u3059\u308b\u3082\u3046\uff11\u3064\u30af\u30a8\u30ea\u30fc<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>SELECT * FROM users\r\n    ORDER BY <strong>(SELECT id FROM posts where user_id = posts.id ORDER BY id desc limit 1)<\/strong> DESC<\/pre>\n<p>\u3069\u3046\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u5c11\u3057\u8907\u96d1\u306b\u898b\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5b9f\u306f\u3053\u308c<strong>\u592a\u5b57\u306e\u62ec\u5f27\u306e\u90e8\u5206\u304c\u3072\u3068\u3064\u306e\u30af\u30a8\u30ea\u30fc\u306b\u306a\u3063\u3066\u3044\u308b\u3060\u3051\u3067\u3001\u3055\u3063\u304d\u306e\u6a19\u6e96\u30af\u30a8\u30ea\u30fc\u3068\u69cb\u9020\u306f\u5168\u304f<\/strong>\u540c\u3058\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3068\u306f<strong>\u300c\u30af\u30a8\u30ea\u30fc\u306e\u4e2d\u306b\u3042\u308b\u30af\u30a8\u30ea\u30fc\u300d<\/strong>\u3068\u3044\u3063\u3066\u3082\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>SELECT * FROM users\r\n    ORDER BY <strong>(\u3053\u3053\u304c\u30b5\u30d6\u30af\u30a8\u30ea\u30fc)<\/strong> DESC<\/pre>\n<h1>\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3067\u4f55\u304c\u3067\u304d\u308b\u306e\u304b<\/h1>\n<p>\u5b9f\u306f\u3053\u308c\u307e\u3067\u3067\u3082<code>Laravel<\/code>\u3067\u306f\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u3070\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<ul>\n<li>selectRaw()<\/li>\n<li>whereRaw()<\/li>\n<li>OrderByRaw();<\/li>\n<\/ul>\n<p>\u305f\u3060\u3057\u3001\u3053\u306e\u5834\u5408\u306f\u90e8\u5206\u7684\u3068\u306f\u3044\u3048\u3001\u3084\u306f\u308a\u81ea\u5206\u3067SQL\u6587\u3092\u66f8\u304f\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u8907\u96d1\u306a\u3082\u306e\u3092\u8a18\u8ff0\u3059\u308b\u3068\u53ef\u8aad\u6027\u304c\u60aa\u304f\u306a\u3063\u3066\u307e\u3057\u305f\u3002<\/p>\n<p>\u3067\u306f\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u4f7f\u3048\u3070\u4f55\u304c\u3067\u304d\u308b\u306e\u304b\u3068\u3044\u3046\u3068\u3001\u4f8b\u3048\u3070\u6b21\u306e\u3088\u3046\u306a\u6761\u4ef6\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30e6\u30fc\u30b6\u30fc\u304c\u53d7\u3051\u305f\u30c6\u30b9\u30c8\u306e\u6700\u9ad8\u70b9\u3082\u8ffd\u52a0\u3057\u3066\u30c7\u30fc\u30bf\u53d6\u5f97<\/li>\n<li>\u4e00\u756a\u306f\u3058\u3081\u306b\u6ce8\u6587\u3057\u305f\u65e5\u6642\u3082\u4e00\u7dd2\u306b\u30c7\u30fc\u30bf\u53d6\u5f97<\/li>\n<li>\u6700\u65b0\u8a18\u4e8b\u306e\u65e5\u6642\u3067\u30e6\u30fc\u30b6\u30fc\u3092\u4e26\u3079\u66ff\u3048<\/li>\n<\/ul>\n<p>\u3064\u307e\u308a\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u5148\u3068\u306e\u9023\u643a\u3092\u3088\u308a\u7d30\u304b\u304f\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h1>\u30c6\u30b9\u30c8\u74b0\u5883\u3092\u3064\u304f\u308b<\/h1>\n<h2>\u524d\u63d0\u3068\u3057\u3066<\/h2>\n<p>\u524d\u63d0\u3068\u3057\u3066<code>Laravel<\/code>\u306e\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b \u307e\u3060\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u306a\u3044\u4eba\u306f\u3001<a href=\"https:\/\/blog.capilano-fw.com\/?p=4576\">Laravel6.0\u3067\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u3092\u4f7f\u3046\u65b9\u6cd5<\/a>\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3084\u308a\u65b9\u304c\u5909\u66f4\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001<code>users<\/code>\u30c6\u30fc\u30d6\u30eb\u3068<code>hasMany<\/code>\u306e\u95a2\u4fc2\u306b\u306a\u308b\u30c6\u30fc\u30d6\u30eb\u306f<strong>\u300cexam_results\u300d<\/strong>\u3067\u3001\u3053\u3053\u306b\u306f\u5404\u30e6\u30fc\u30b6\u30fc\u304c\u53d7\u3051\u305f\u30c6\u30b9\u30c8\u306e\u7d50\u679c\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n<p>\u30a4\u30e1\u30fc\u30b8\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<p><strong>\u592a\u90ce\u3055\u3093\uff1a<\/strong><br \/>\n\u30fb90\u70b9\uff08\u53d7\u9a13\u65e5: 2019.09.01\uff09<br \/>\n\u30fb73\u70b9\uff08\u53d7\u9a13\u65e5: 2019.08.15\uff09<\/p>\n<p><strong>\u6b21\u90ce\u3055\u3093\uff1a<\/strong><br \/>\n\u30fb30\u70b9\uff08\u53d7\u9a13\u65e5: 2019.09.03\uff09<br \/>\n\u30fb63\u70b9\uff08\u53d7\u9a13\u65e5: 2019.08.07\uff09<br \/>\n\u30fb72\u70b9\uff08\u53d7\u9a13\u65e5: 2019.08.01\uff09<\/p>\n<p><strong>\u4e09\u90ce\u3055\u3093\uff1a<\/strong><br \/>\n\u30fb98\u70b9\uff08\u53d7\u9a13\u65e5: 2019.08.27\uff09<\/p>\n<h2>\u30e2\u30c7\u30eb\u3068\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308b<\/h2>\n<p>\u3067\u306f\u3001\u307e\u305a\u306f\u30c6\u30b9\u30c8\u7d50\u679c\u3092\u7ba1\u7406\u3059\u308b\u30e2\u30c7\u30eb\u3068\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308a\u307e\u3057\u3087\u3046\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:model ExamResult -m<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001<code>database\/migrations\/****_**_**_******_create_exam_results_table.php<\/code>\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nuse Illuminate\\Support\\Facades\\Schema;\r\nuse Illuminate\\Database\\Schema\\Blueprint;\r\nuse Illuminate\\Database\\Migrations\\Migration;\r\n\r\nclass CreateExamResultsTable extends Migration\r\n{\r\n    \/**\r\n     * Run the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function up()\r\n    {\r\n        <strong>Schema::create('exam_results', function (Blueprint $table) {<\/strong>\r\n<strong>            $table-&gt;bigIncrements('id');<\/strong>\r\n<strong>            $table-&gt;unsignedBigInteger('user_id');<\/strong>\r\n<strong>            $table-&gt;integer('score');<\/strong>\r\n<strong>            $table-&gt;timestamps();<\/strong>\r\n\r\n<strong>            $table-&gt;foreign('user_id')<\/strong>\r\n<strong>                -&gt;references('id')-&gt;on('users')<\/strong>\r\n<strong>                -&gt;onDelete('cascade');<\/strong>\r\n<strong>        });<\/strong>\r\n    }\r\n\r\n    \/**\r\n     * Reverse the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function down()\r\n    {\r\n        Schema::dropIfExists('exam_results');\r\n    }\r\n}<\/pre>\n<h2>Seeder\u3092\u3064\u304f\u308b<\/h2>\n<p>\u7d9a\u3044\u3066\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092<code>Seeder<\/code>\u3067\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:seeder ExamResultsTableSeeder<\/strong><\/pre>\n<p><code>database\/seeds\/ExamResultsTableSeeder.php<\/code>\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nuse Illuminate\\Database\\Seeder;\r\n\r\nclass ExamResultsTableSeeder extends Seeder\r\n{\r\n    \/**\r\n     * Run the database seeds.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function run()\r\n    {\r\n        <strong>$user_ids = \\App\\User::pluck('id');<\/strong>\r\n\r\n<strong>        for($i = 0 ; $i &lt; 100 ; $i++) {<\/strong>\r\n\r\n<strong>            $exam_result = new \\App\\ExamResult();<\/strong>\r\n<strong>            $exam_result-&gt;user_id = $user_ids-&gt;random();<\/strong>\r\n<strong>            $exam_result-&gt;score = rand(30, 100);<\/strong>\r\n<strong>            $exam_result-&gt;save();<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n    }\r\n}<\/pre>\n<p><code>Seeder<\/code>\u304c\u4f5c\u6210\u3067\u304d\u305f\u3089\u3001<code>database\/seeds\/DatabaseSeeder.php<\/code>\u306b\u5fd8\u308c\u305a\u767b\u9332\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>&lt;?php\r\n\r\nuse Illuminate\\Database\\Seeder;\r\n\r\nclass DatabaseSeeder extends Seeder\r\n{\r\n    \/**\r\n     * Seed the application's database.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function run()\r\n    {\r\n         \/\/ \u7701\u7565\r\n\r\n         <strong>$this-&gt;call(ExamResultsTableSeeder::class);<\/strong>\r\n    }\r\n}<\/pre>\n<p>\u3067\u306f\u3001\u3053\u306e\u72b6\u614b\u3067<code>Seeder<\/code>\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>php artisan migrate:fresh --seed<\/strong><\/pre>\n<p>\u203b \u4eca\u56de\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u306f\u3058\u3081\u304b\u3089\u3084\u308a\u76f4\u3059\u5f62\u306b\u3057\u3066\u3044\u307e\u3059\u304c<code>Seeder<\/code>\u306e\u5b9f\u884c\u3060\u3051\u306e\u5834\u5408\u306f<code>php artisan db:seed<\/code>\u3092\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><code>Seeder<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4598\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_1.png\" alt=\"\" width=\"492\" height=\"647\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_1.png 492w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_1-228x300.png 228w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/p>\n<h1 id=\"subquery_usage\">Laravel 6.0\u306e\u65b0\u3057\u3044\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u306e\u4f7f\u3044\u65b9<\/h1>\n<p>\u3067\u306f\u3001\u524d\u7f6e\u304d\u304c\u9577\u304f\u306a\u308a\u307e\u3057\u305f\u304c\u3053\u3053\u304b\u3089\u304c\u30e1\u30a4\u30f3\u306e\u65b0\u3057\u3044\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u306e\u4f7f\u3044\u65b9\u3067\u3059\u3002<\/p>\n<h2>SELECT\u53e5\u3067\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u4f7f\u3046<\/h2>\n<p>\u4f8b\u3048\u3070\u3001<code>users<\/code>\u306e\u30c7\u30fc\u30bf\u306b\u52a0\u3048\u3066<strong>\u300c\u305d\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u53d6\u5f97\u3057\u305f\u6700\u9ad8\u70b9\u300d<\/strong>\u3082\u4e00\u7dd2\u306b\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n<pre><strong>$users = \\App\\User::addSelect(['max_score' =&gt; \\App\\ExamResult::select('score')<\/strong>\r\n<strong>    -&gt;whereColumn('user_id', 'users.id')<\/strong>\r\n<strong>    -&gt;orderBy('score', 'desc')<\/strong>\r\n<strong>    -&gt;take(1)<\/strong>\r\n<strong>])-&gt;get();\r\n\r\ndump($users-&gt;toArray());\r\n<\/strong><\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u306f\u3053\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4600\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_2.png\" alt=\"\" width=\"253\" height=\"328\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_2.png 253w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_2-231x300.png 231w\" sizes=\"auto, (max-width: 253px) 100vw, 253px\" \/><\/p>\n<p>\u5143\u3005<code>users<\/code>\u306b\u306f<code>max_score<\/code>\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3067\u53d6\u5f97\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u306a\u304a\u3001<code>whereColumn()<\/code>\u3067\uff12\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u76ee\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306b\u3088\u3063\u3066\u300c\u5404\u30e6\u30fc\u30b6\u30fc\u306e\u5f97\u70b9\u30c7\u30fc\u30bf\u300d\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>whereColumn('user_id', 'users.id')<\/strong><\/pre>\n<p>\u307e\u305f\u3001\u57fa\u672c\u7684\u306b\u300c\u30af\u30a8\u30ea\u30fc\u300d\u306e\u6307\u5b9a\u306a\u306e\u3067\u3001<code>first()<\/code>\u306a\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3092<strong>\u8a18\u8ff0\u3057\u3066\u3044\u306a\u3044<\/strong>\u3053\u3068\u3082\u899a\u3048\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>ORDER BY\u53e5\u3067\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u4f7f\u3046<\/h2>\n<p>\u7d9a\u3044\u3066\u306f\u7d50\u5408\u5148\u306e\u30c7\u30fc\u30bf\u306b\u3088\u3063\u3066\u4e26\u3079\u66ff\u3048\u3092\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<pre><strong>$users = \\App\\User::orderByDesc(<\/strong>\r\n<strong>    \\App\\ExamResult::select('score')<\/strong>\r\n<strong>        -&gt;whereColumn('user_id', 'users.id')<\/strong>\r\n<strong>        -&gt;orderBy('score', 'desc')<\/strong>\r\n<strong>        -&gt;take(1)<\/strong>\r\n<strong>)-&gt;get();<\/strong>\r\n\r\n<strong>dump($users-&gt;toArray());<\/strong><\/pre>\n<p>\u5b9f\u884c\u3057\u305f\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\uff08\u305f\u3060\u3057\u3053\u306e\u5834\u5408\u3001\u5b9f\u969b\u306b\u306f\u308f\u304b\u308a\u3084\u3059\u3044\u3088\u3046\u306b\u6700\u9ad8\u70b9\u3082\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\uff09<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4601\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_3.png\" alt=\"\" width=\"281\" height=\"532\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_3.png 281w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/sub_query_3-158x300.png 158w\" sizes=\"auto, (max-width: 281px) 100vw, 281px\" \/><\/p>\n<p>\u306a\u304a\u3001\u30b5\u30f3\u30d7\u30eb\u3067\u306f<code>orderByDesc()<\/code>\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u304c\u901a\u5e38\u306e<code>orderBy()<\/code>\u3082\u3082\u3061\u308d\u3093\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u3088\u308a\u4f7f\u3044\u3084\u3059\u304f\u306a\u3063\u305f<code>Laravel<\/code>\u306e\u300c\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u300d\u306b\u3064\u3044\u3066\u8a18\u4e8b\u3092\u304a\u5c4a\u3051\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u306a\u305c\u79c1\u304c\u8a18\u4e8b\u306e\u5192\u982d\u3067\u300c\u9577\u5e74\u5e0c\u671b\u3057\u3066\u3044\u305f\u300d\u3068\u66f8\u3044\u305f\u304b\u3068\u3044\u3046\u3068\u3001\u5b9f\u969b\u306b\u958b\u767a\u3059\u308b\u969b\u306b\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u69d8\u65b9\u304b\u3089\u306e\u3054\u8981\u671b\u3067<strong>\u300c\u7d50\u5408\u5148\u306e\u60c5\u5831\u3067\u4e26\u3079\u66ff\u3048\u3092\u3059\u308b\u300d<\/strong>\u3068\u3044\u3046\u6a5f\u80fd\u304c\u5fc5\u8981\u306b\u306a\u308b\u3053\u3068\u304c\u591a\u304b\u3063\u305f\u304b\u3089\u3067\u3059\u3002\uff08\u691c\u7d22\u30da\u30fc\u30b8\u3084\u4e00\u89a7\u30da\u30fc\u30b8\u3067\u3059\u306d\uff09<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u3053\u308c\u307e\u3067\u306f<code>JOIN()<\/code>\u3092\u4f7f\u3063\u3066\uff12\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3066\u305f\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u304c\u30b3\u30fc\u30c9\u91cf\u304c\u591a\u304f\u306a\u308b\u308f\u3001\u53ef\u8aad\u6027\u306f\u843d\u3061\u308b\u308f\u3067\u3069\u3046\u3057\u305f\u3082\u3093\u304b\u3068\u3044\u3064\u3082\u8003\u3048\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u4e00\u6642\u671f\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>FIELD<\/code>\u95a2\u6570\u3092\u4f7f\u3063\u305f\u308a\u3082\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u3069\u3046\u3084\u3089<code>MySQL<\/code>\u3060\u3051\u306e\u6a5f\u80fd\u3060\u3063\u305f\u306e\u3067\u4f7f\u3046\u306e\u3092\u6975\u529b\u3084\u3081\u3066\u3044\u305f\u3068\u3044\u3046\u7d4c\u7def\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>ORDER BY FIELD(id, 3, 2, 1, 4);<\/strong><\/pre>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u5f8c\u306f\u3053\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u5b9f\u969b\u306e\u958b\u767a\u3067\u3082\u4f7f\u3063\u3066\u3044\u304f\u3064\u3082\u308a\u3067\u3059\u3002<\/p>\n<p>\u7686\u3055\u3093\u3082\u65b0\u3057\u3044\u4e00\u5ea6\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u306e\u66f8\u304d\u65b9\u3092\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3467 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png\" alt=\"\" width=\"216\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png 216w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9-203x300.png 203w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u524d\u56de\u30fb\u524d\u3005\u56de\u3068Laravel\u306e\u65b0\u30d0\u30fc\u30b8\u30e7\u30f36.0\u306e\u65b0\u6a5f\u80fd\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3092\u3057\u3066\u304d\u307e\u3057\u305f\u3002 \u4eca\u56de\u306f\u305d\u306e\uff13\u5f3e\u3067\u3059\u304c\u3001\u65b0\u6a5f\u80fd\u3068\u3044\u3046\u3088\u308a\u306f\u4ee5\u524d\u304b\u3089\u3042\u3063\u305f\u6a5f\u80fd\u3092\u5f37\u5316 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=4596\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel 6.0 \u306e\u65b0\u3057\u3044\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3092\u7d39\u4ecb&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":4603,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[175,174,60],"class_list":["post-4596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-sql","tag-174","tag-60"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4596"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4596\/revisions"}],"predecessor-version":[{"id":4606,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4596\/revisions\/4606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/4603"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}