回答編集履歴
4
追記
test
CHANGED
@@ -1,466 +1,6 @@
|
|
1
|
-
|
1
|
+
==== 消した ====
|
2
2
|
|
3
3
|
|
4
|
-
|
5
|
-
likes テーブルの役割は、Post と User を結ぶ多対多のピポットテーブルですね。
|
6
|
-
|
7
|
-
定義を見る限り。
|
8
|
-
|
9
|
-
HasMany でもできなくはないけど。
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
手元の環境は BelongsToMany で作ったのでそれを参考にしてください。
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
User は全てデフォルトのままなので省略します。
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
手元の環境では、これで正常に動作している。
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
# DB
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
```php
|
30
|
-
|
31
|
-
<?php
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
use Illuminate\Database\Migrations\Migration;
|
36
|
-
|
37
|
-
use Illuminate\Database\Schema\Blueprint;
|
38
|
-
|
39
|
-
use Illuminate\Support\Facades\Schema;
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
class CreatePostsTable extends Migration
|
44
|
-
|
45
|
-
{
|
46
|
-
|
47
|
-
/**
|
48
|
-
|
49
|
-
* Run the migrations.
|
50
|
-
|
51
|
-
*
|
52
|
-
|
53
|
-
* @return void
|
54
|
-
|
55
|
-
*/
|
56
|
-
|
57
|
-
public function up()
|
58
|
-
|
59
|
-
{
|
60
|
-
|
61
|
-
Schema::create('posts', function (Blueprint $table) {
|
62
|
-
|
63
|
-
$table->bigIncrements('id');
|
64
|
-
|
65
|
-
$table->string('title');
|
66
|
-
|
67
|
-
$table->string('content');
|
68
|
-
|
69
|
-
$table->timestamps();
|
70
|
-
|
71
|
-
});
|
72
|
-
|
73
|
-
}
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
/**
|
78
|
-
|
79
|
-
* Reverse the migrations.
|
80
|
-
|
81
|
-
*
|
82
|
-
|
83
|
-
* @return void
|
84
|
-
|
85
|
-
*/
|
86
|
-
|
87
|
-
public function down()
|
88
|
-
|
89
|
-
{
|
90
|
-
|
91
|
-
Schema::dropIfExists('posts');
|
92
|
-
|
93
|
-
}
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
```
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
```php
|
102
|
-
|
103
|
-
<?php
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
use Illuminate\Database\Migrations\Migration;
|
108
|
-
|
109
|
-
use Illuminate\Database\Schema\Blueprint;
|
110
|
-
|
111
|
-
use Illuminate\Support\Facades\Schema;
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
class CreateLikesTable extends Migration
|
116
|
-
|
117
|
-
{
|
118
|
-
|
119
|
-
/**
|
120
|
-
|
121
|
-
* Run the migrations.
|
122
|
-
|
123
|
-
*
|
124
|
-
|
125
|
-
* @return void
|
126
|
-
|
127
|
-
*/
|
128
|
-
|
129
|
-
public function up()
|
130
|
-
|
131
|
-
{
|
132
|
-
|
133
|
-
Schema::create('likes', function (Blueprint $table) {
|
134
|
-
|
135
|
-
$table->bigIncrements('id');
|
136
|
-
|
137
|
-
$table->unsignedBigInteger('post_id');
|
138
|
-
|
139
|
-
$table->unsignedBigInteger('user_id');
|
140
|
-
|
141
|
-
$table->timestamps();
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
$table->foreign('post_id')->references('id')->on('posts');
|
146
|
-
|
147
|
-
$table->foreign('user_id')->references('id')->on('users');
|
148
|
-
|
149
|
-
});
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
/**
|
156
|
-
|
157
|
-
* Reverse the migrations.
|
158
|
-
|
159
|
-
*
|
160
|
-
|
161
|
-
* @return void
|
162
|
-
|
163
|
-
*/
|
164
|
-
|
165
|
-
public function down()
|
166
|
-
|
167
|
-
{
|
168
|
-
|
169
|
-
Schema::dropIfExists('likes');
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
```
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
# Model
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
```php
|
184
|
-
|
185
|
-
<?php
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
namespace App;
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
use Carbon\Traits\Timestamp;
|
194
|
-
|
195
|
-
use Illuminate\Database\Eloquent\Model;
|
196
|
-
|
197
|
-
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
class Post extends Model
|
202
|
-
|
203
|
-
{
|
204
|
-
|
205
|
-
use Timestamp;
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
/**
|
210
|
-
|
211
|
-
* @return BelongsToMany|User[]
|
212
|
-
|
213
|
-
*/
|
214
|
-
|
215
|
-
public function likes(): BelongsToMany
|
216
|
-
|
217
|
-
{
|
218
|
-
|
219
|
-
return $this->belongsToMany(User::class, 'likes')
|
220
|
-
|
221
|
-
->withTimestamps();
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
```
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
# Factory
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
```php
|
236
|
-
|
237
|
-
<?php
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
use App\Post;
|
246
|
-
|
247
|
-
use Faker\Generator as Faker;
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
$factory->define(Post::class, function (Faker $faker) {
|
252
|
-
|
253
|
-
return [
|
254
|
-
|
255
|
-
'title' => $faker->word,
|
256
|
-
|
257
|
-
'content' => $faker->sentence
|
258
|
-
|
259
|
-
];
|
260
|
-
|
261
|
-
});
|
262
|
-
|
263
|
-
```
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
# Seeder
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
```php
|
272
|
-
|
273
|
-
<?php
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
use App\Post;
|
278
|
-
|
279
|
-
use App\User;
|
280
|
-
|
281
|
-
use Illuminate\Database\Seeder;
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
class PostSeeder extends Seeder
|
286
|
-
|
287
|
-
{
|
288
|
-
|
289
|
-
/**
|
290
|
-
|
291
|
-
* Run the database seeds.
|
292
|
-
|
293
|
-
*
|
294
|
-
|
295
|
-
* @return void
|
296
|
-
|
297
|
-
*/
|
298
|
-
|
299
|
-
public function run()
|
300
|
-
|
301
|
-
{
|
302
|
-
|
303
|
-
/** @var User[] users */
|
304
|
-
|
305
|
-
$users = factory(User::class, 20)
|
306
|
-
|
307
|
-
->create();
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
$this->posts = factory(Post::class, 100)
|
312
|
-
|
313
|
-
->create()
|
314
|
-
|
315
|
-
->each(function (Post $post) use ($users) {
|
316
|
-
|
317
|
-
$post->likes()->attach(
|
318
|
-
|
319
|
-
$users->random(random_int(1, 3))->pluck('id')->toArray()
|
320
|
-
|
321
|
-
);
|
322
|
-
|
323
|
-
});
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
```
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
# Controller
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
```php
|
338
|
-
|
339
|
-
<?php
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
namespace App\Http\Controllers;
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
use App\Post;
|
348
|
-
|
349
|
-
use Illuminate\Http\Request;
|
350
|
-
|
351
|
-
use Illuminate\View\View;
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
class PostController extends Controller
|
356
|
-
|
357
|
-
{
|
358
|
-
|
359
|
-
/**
|
360
|
-
|
361
|
-
* Display a listing of the resource.
|
362
|
-
|
363
|
-
*
|
364
|
-
|
365
|
-
* @return View
|
366
|
-
|
367
|
-
*/
|
368
|
-
|
369
|
-
public function index(): View
|
370
|
-
|
371
|
-
{
|
372
|
-
|
373
|
-
$posts = Post::query()
|
374
|
-
|
375
|
-
->withCount(['likes'])
|
376
|
-
|
377
|
-
->paginate();
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
return view('posts.index', compact('posts'));
|
382
|
-
|
383
|
-
}
|
384
|
-
|
385
|
-
}
|
386
|
-
|
387
|
-
```
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
# View
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
```php
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
<table>
|
400
|
-
|
401
|
-
<tbody>
|
402
|
-
|
403
|
-
@foreach($posts as $post)
|
404
|
-
|
405
|
-
<tr>
|
406
|
-
|
407
|
-
<td>{{ $post->id }}</td>
|
408
|
-
|
409
|
-
<td>{{ $post->likes_count }}</td>
|
410
|
-
|
411
|
-
</tr>
|
412
|
-
|
413
|
-
@endforeach()
|
414
|
-
|
415
|
-
</tbody>
|
416
|
-
|
417
|
-
</table>
|
418
|
-
|
419
|
-
```
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
# SQLエラーの確認方法
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
```php
|
428
|
-
|
429
|
-
$posts = Post::query()
|
430
|
-
|
431
|
-
->withCount(['likes'])
|
432
|
-
|
433
|
-
->paginate();
|
434
|
-
|
435
|
-
```
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
このコードで仮に、エラーが出たとしよう。
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
```diff
|
444
|
-
|
445
|
-
$posts = Post::query()
|
446
|
-
|
447
|
-
->withCount(['likes'])
|
448
|
-
|
449
|
-
- ->paginate();
|
450
|
-
|
451
|
-
+ ->toSql();
|
452
|
-
|
453
|
-
+ dd($posts);
|
454
|
-
|
455
|
-
```
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
こんなふうにすれば、組み立てられたSQLを確認できる
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
# 余談
|
464
4
|
|
465
5
|
|
466
6
|
|
3
追記
test
CHANGED
@@ -420,6 +420,46 @@
|
|
420
420
|
|
421
421
|
|
422
422
|
|
423
|
+
# SQLエラーの確認方法
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
```php
|
428
|
+
|
429
|
+
$posts = Post::query()
|
430
|
+
|
431
|
+
->withCount(['likes'])
|
432
|
+
|
433
|
+
->paginate();
|
434
|
+
|
435
|
+
```
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
このコードで仮に、エラーが出たとしよう。
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
```diff
|
444
|
+
|
445
|
+
$posts = Post::query()
|
446
|
+
|
447
|
+
->withCount(['likes'])
|
448
|
+
|
449
|
+
- ->paginate();
|
450
|
+
|
451
|
+
+ ->toSql();
|
452
|
+
|
453
|
+
+ dd($posts);
|
454
|
+
|
455
|
+
```
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
こんなふうにすれば、組み立てられたSQLを確認できる
|
460
|
+
|
461
|
+
|
462
|
+
|
423
463
|
# 余談
|
424
464
|
|
425
465
|
|
2
修正
test
CHANGED
@@ -424,8 +424,24 @@
|
|
424
424
|
|
425
425
|
|
426
426
|
|
427
|
-
Laravel の開発にあたっては、以下のプ
|
427
|
+
Laravel の開発にあたっては、以下のプラグインは必ずインストすべし。
|
428
428
|
|
429
429
|
|
430
430
|
|
431
431
|
[https://github.com/barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
基本的に、LaravelのDB周りの処理は、 Eloquent で書くべき。
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
`DB::select()`なんて滅多に使う場面がない。よほど、Eloquent では十分なパフォーマンスが出ない場合などに限られる。DBに用意されているメソッドはEloquentでも使えるのだから、Eloquentでかけないはずがない。
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
Laravel のベストプラクティスがまとめられているので、参考にすると良いでしょう。
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
[https://github.com/alexeymezenin/laravel-best-practices](https://github.com/alexeymezenin/laravel-best-practices/blob/master/japanese.md)
|
1
追記
test
CHANGED
@@ -417,3 +417,15 @@
|
|
417
417
|
</table>
|
418
418
|
|
419
419
|
```
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
# 余談
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
Laravel の開発にあたっては、以下のプッラグインは必ずインストすべし。
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
[https://github.com/barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
|