回答編集履歴
4
追記
answer
CHANGED
@@ -1,236 +1,6 @@
|
|
1
|
-
|
1
|
+
==== 消した ====
|
2
2
|
|
3
|
-
likes テーブルの役割は、Post と User を結ぶ多対多のピポットテーブルですね。
|
4
|
-
定義を見る限り。
|
5
|
-
HasMany でもできなくはないけど。
|
6
3
|
|
7
|
-
手元の環境は BelongsToMany で作ったのでそれを参考にしてください。
|
8
|
-
|
9
|
-
User は全てデフォルトのままなので省略します。
|
10
|
-
|
11
|
-
手元の環境では、これで正常に動作している。
|
12
|
-
|
13
|
-
# DB
|
14
|
-
|
15
|
-
```php
|
16
|
-
<?php
|
17
|
-
|
18
|
-
use Illuminate\Database\Migrations\Migration;
|
19
|
-
use Illuminate\Database\Schema\Blueprint;
|
20
|
-
use Illuminate\Support\Facades\Schema;
|
21
|
-
|
22
|
-
class CreatePostsTable extends Migration
|
23
|
-
{
|
24
|
-
/**
|
25
|
-
* Run the migrations.
|
26
|
-
*
|
27
|
-
* @return void
|
28
|
-
*/
|
29
|
-
public function up()
|
30
|
-
{
|
31
|
-
Schema::create('posts', function (Blueprint $table) {
|
32
|
-
$table->bigIncrements('id');
|
33
|
-
$table->string('title');
|
34
|
-
$table->string('content');
|
35
|
-
$table->timestamps();
|
36
|
-
});
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Reverse the migrations.
|
41
|
-
*
|
42
|
-
* @return void
|
43
|
-
*/
|
44
|
-
public function down()
|
45
|
-
{
|
46
|
-
Schema::dropIfExists('posts');
|
47
|
-
}
|
48
|
-
}
|
49
|
-
```
|
50
|
-
|
51
|
-
```php
|
52
|
-
<?php
|
53
|
-
|
54
|
-
use Illuminate\Database\Migrations\Migration;
|
55
|
-
use Illuminate\Database\Schema\Blueprint;
|
56
|
-
use Illuminate\Support\Facades\Schema;
|
57
|
-
|
58
|
-
class CreateLikesTable extends Migration
|
59
|
-
{
|
60
|
-
/**
|
61
|
-
* Run the migrations.
|
62
|
-
*
|
63
|
-
* @return void
|
64
|
-
*/
|
65
|
-
public function up()
|
66
|
-
{
|
67
|
-
Schema::create('likes', function (Blueprint $table) {
|
68
|
-
$table->bigIncrements('id');
|
69
|
-
$table->unsignedBigInteger('post_id');
|
70
|
-
$table->unsignedBigInteger('user_id');
|
71
|
-
$table->timestamps();
|
72
|
-
|
73
|
-
$table->foreign('post_id')->references('id')->on('posts');
|
74
|
-
$table->foreign('user_id')->references('id')->on('users');
|
75
|
-
});
|
76
|
-
}
|
77
|
-
|
78
|
-
/**
|
79
|
-
* Reverse the migrations.
|
80
|
-
*
|
81
|
-
* @return void
|
82
|
-
*/
|
83
|
-
public function down()
|
84
|
-
{
|
85
|
-
Schema::dropIfExists('likes');
|
86
|
-
}
|
87
|
-
}
|
88
|
-
```
|
89
|
-
|
90
|
-
# Model
|
91
|
-
|
92
|
-
```php
|
93
|
-
<?php
|
94
|
-
|
95
|
-
namespace App;
|
96
|
-
|
97
|
-
use Carbon\Traits\Timestamp;
|
98
|
-
use Illuminate\Database\Eloquent\Model;
|
99
|
-
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
100
|
-
|
101
|
-
class Post extends Model
|
102
|
-
{
|
103
|
-
use Timestamp;
|
104
|
-
|
105
|
-
/**
|
106
|
-
* @return BelongsToMany|User[]
|
107
|
-
*/
|
108
|
-
public function likes(): BelongsToMany
|
109
|
-
{
|
110
|
-
return $this->belongsToMany(User::class, 'likes')
|
111
|
-
->withTimestamps();
|
112
|
-
}
|
113
|
-
}
|
114
|
-
```
|
115
|
-
|
116
|
-
# Factory
|
117
|
-
|
118
|
-
```php
|
119
|
-
<?php
|
120
|
-
|
121
|
-
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
122
|
-
|
123
|
-
use App\Post;
|
124
|
-
use Faker\Generator as Faker;
|
125
|
-
|
126
|
-
$factory->define(Post::class, function (Faker $faker) {
|
127
|
-
return [
|
128
|
-
'title' => $faker->word,
|
129
|
-
'content' => $faker->sentence
|
130
|
-
];
|
131
|
-
});
|
132
|
-
```
|
133
|
-
|
134
|
-
# Seeder
|
135
|
-
|
136
|
-
```php
|
137
|
-
<?php
|
138
|
-
|
139
|
-
use App\Post;
|
140
|
-
use App\User;
|
141
|
-
use Illuminate\Database\Seeder;
|
142
|
-
|
143
|
-
class PostSeeder extends Seeder
|
144
|
-
{
|
145
|
-
/**
|
146
|
-
* Run the database seeds.
|
147
|
-
*
|
148
|
-
* @return void
|
149
|
-
*/
|
150
|
-
public function run()
|
151
|
-
{
|
152
|
-
/** @var User[] users */
|
153
|
-
$users = factory(User::class, 20)
|
154
|
-
->create();
|
155
|
-
|
156
|
-
$this->posts = factory(Post::class, 100)
|
157
|
-
->create()
|
158
|
-
->each(function (Post $post) use ($users) {
|
159
|
-
$post->likes()->attach(
|
160
|
-
$users->random(random_int(1, 3))->pluck('id')->toArray()
|
161
|
-
);
|
162
|
-
});
|
163
|
-
}
|
164
|
-
}
|
165
|
-
```
|
166
|
-
|
167
|
-
# Controller
|
168
|
-
|
169
|
-
```php
|
170
|
-
<?php
|
171
|
-
|
172
|
-
namespace App\Http\Controllers;
|
173
|
-
|
174
|
-
use App\Post;
|
175
|
-
use Illuminate\Http\Request;
|
176
|
-
use Illuminate\View\View;
|
177
|
-
|
178
|
-
class PostController extends Controller
|
179
|
-
{
|
180
|
-
/**
|
181
|
-
* Display a listing of the resource.
|
182
|
-
*
|
183
|
-
* @return View
|
184
|
-
*/
|
185
|
-
public function index(): View
|
186
|
-
{
|
187
|
-
$posts = Post::query()
|
188
|
-
->withCount(['likes'])
|
189
|
-
->paginate();
|
190
|
-
|
191
|
-
return view('posts.index', compact('posts'));
|
192
|
-
}
|
193
|
-
}
|
194
|
-
```
|
195
|
-
|
196
|
-
# View
|
197
|
-
|
198
|
-
```php
|
199
|
-
|
200
|
-
<table>
|
201
|
-
<tbody>
|
202
|
-
@foreach($posts as $post)
|
203
|
-
<tr>
|
204
|
-
<td>{{ $post->id }}</td>
|
205
|
-
<td>{{ $post->likes_count }}</td>
|
206
|
-
</tr>
|
207
|
-
@endforeach()
|
208
|
-
</tbody>
|
209
|
-
</table>
|
210
|
-
```
|
211
|
-
|
212
|
-
# SQLエラーの確認方法
|
213
|
-
|
214
|
-
```php
|
215
|
-
$posts = Post::query()
|
216
|
-
->withCount(['likes'])
|
217
|
-
->paginate();
|
218
|
-
```
|
219
|
-
|
220
|
-
このコードで仮に、エラーが出たとしよう。
|
221
|
-
|
222
|
-
```diff
|
223
|
-
$posts = Post::query()
|
224
|
-
->withCount(['likes'])
|
225
|
-
- ->paginate();
|
226
|
-
+ ->toSql();
|
227
|
-
+ dd($posts);
|
228
|
-
```
|
229
|
-
|
230
|
-
こんなふうにすれば、組み立てられたSQLを確認できる
|
231
|
-
|
232
|
-
# 余談
|
233
|
-
|
234
4
|
Laravel の開発にあたっては、以下のプラグインは必ずインストすべし。
|
235
5
|
|
236
6
|
[https://github.com/barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
|
3
追記
answer
CHANGED
@@ -209,6 +209,26 @@
|
|
209
209
|
</table>
|
210
210
|
```
|
211
211
|
|
212
|
+
# SQLエラーの確認方法
|
213
|
+
|
214
|
+
```php
|
215
|
+
$posts = Post::query()
|
216
|
+
->withCount(['likes'])
|
217
|
+
->paginate();
|
218
|
+
```
|
219
|
+
|
220
|
+
このコードで仮に、エラーが出たとしよう。
|
221
|
+
|
222
|
+
```diff
|
223
|
+
$posts = Post::query()
|
224
|
+
->withCount(['likes'])
|
225
|
+
- ->paginate();
|
226
|
+
+ ->toSql();
|
227
|
+
+ dd($posts);
|
228
|
+
```
|
229
|
+
|
230
|
+
こんなふうにすれば、組み立てられたSQLを確認できる
|
231
|
+
|
212
232
|
# 余談
|
213
233
|
|
214
234
|
Laravel の開発にあたっては、以下のプラグインは必ずインストすべし。
|
2
修正
answer
CHANGED
@@ -211,6 +211,14 @@
|
|
211
211
|
|
212
212
|
# 余談
|
213
213
|
|
214
|
-
Laravel の開発にあたっては、以下のプ
|
214
|
+
Laravel の開発にあたっては、以下のプラグインは必ずインストすべし。
|
215
215
|
|
216
|
-
[https://github.com/barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
|
216
|
+
[https://github.com/barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
|
217
|
+
|
218
|
+
基本的に、LaravelのDB周りの処理は、 Eloquent で書くべき。
|
219
|
+
|
220
|
+
`DB::select()`なんて滅多に使う場面がない。よほど、Eloquent では十分なパフォーマンスが出ない場合などに限られる。DBに用意されているメソッドはEloquentでも使えるのだから、Eloquentでかけないはずがない。
|
221
|
+
|
222
|
+
Laravel のベストプラクティスがまとめられているので、参考にすると良いでしょう。
|
223
|
+
|
224
|
+
[https://github.com/alexeymezenin/laravel-best-practices](https://github.com/alexeymezenin/laravel-best-practices/blob/master/japanese.md)
|
1
追記
answer
CHANGED
@@ -207,4 +207,10 @@
|
|
207
207
|
@endforeach()
|
208
208
|
</tbody>
|
209
209
|
</table>
|
210
|
-
```
|
210
|
+
```
|
211
|
+
|
212
|
+
# 余談
|
213
|
+
|
214
|
+
Laravel の開発にあたっては、以下のプッラグインは必ずインストすべし。
|
215
|
+
|
216
|
+
[https://github.com/barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
|