質問編集履歴
3
インデントされていコードがあったので修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -184,69 +184,69 @@
|
|
184
184
|
|
185
185
|
<header>
|
186
186
|
|
187
|
-
<nav class="navbar navbar-inverse navbar-static-top">
|
188
|
-
|
189
|
-
<div class="container">
|
190
|
-
|
191
|
-
<div class="navbar-header">
|
192
|
-
|
193
|
-
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
|
194
|
-
|
195
|
-
<span class="sr-only">Toggle navigation</span>
|
196
|
-
|
197
|
-
<span class="icon-bar"></span>
|
198
|
-
|
199
|
-
<span class="icon-bar"></span>
|
200
|
-
|
201
|
-
<span class="icon-bar"></span>
|
202
|
-
|
203
|
-
</button>
|
204
|
-
|
205
|
-
<a class="navbar-brand" href="/">Microposts</a>
|
206
|
-
|
207
|
-
</div>
|
208
|
-
|
209
|
-
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
210
|
-
|
211
|
-
<ul class="nav navbar-nav navbar-right">
|
212
|
-
|
213
|
-
@if (Auth::check())
|
214
|
-
|
215
|
-
<li>{!! link_to_route('users.index', 'Users') !!}</li>
|
216
|
-
|
217
|
-
<li class="dropdown">
|
218
|
-
|
219
|
-
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ Auth::user()->name }} <span class="caret"></span></a>
|
220
|
-
|
221
|
-
<ul class="dropdown-menu">
|
222
|
-
|
223
|
-
<li>{!! link_to_route('users.show', 'My profile', ['id' => $user->id]) !!}</li>
|
224
|
-
|
225
|
-
<li>{!! link_to_route('micropost.favorites', 'Favorites') !!}</li> <!--TODO-->
|
226
|
-
|
227
|
-
<li role="separator" class="divider"></li>
|
228
|
-
|
229
|
-
<li>{!! link_to_route('logout.get', 'Logout') !!}</li>
|
230
|
-
|
231
|
-
</ul>
|
232
|
-
|
233
|
-
</li>
|
234
|
-
|
235
|
-
@else
|
236
|
-
|
237
|
-
<li>{!! link_to_route('signup.get', 'Signup') !!}</li>
|
238
|
-
|
239
|
-
<li>{!! link_to_route('login.get', 'Login') !!}</li>
|
240
|
-
|
241
|
-
@endif
|
242
|
-
|
243
|
-
</ul>
|
244
|
-
|
245
|
-
</div>
|
246
|
-
|
247
|
-
</div>
|
248
|
-
|
249
|
-
</nav>
|
187
|
+
<nav class="navbar navbar-inverse navbar-static-top">
|
188
|
+
|
189
|
+
<div class="container">
|
190
|
+
|
191
|
+
<div class="navbar-header">
|
192
|
+
|
193
|
+
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
|
194
|
+
|
195
|
+
<span class="sr-only">Toggle navigation</span>
|
196
|
+
|
197
|
+
<span class="icon-bar"></span>
|
198
|
+
|
199
|
+
<span class="icon-bar"></span>
|
200
|
+
|
201
|
+
<span class="icon-bar"></span>
|
202
|
+
|
203
|
+
</button>
|
204
|
+
|
205
|
+
<a class="navbar-brand" href="/">Microposts</a>
|
206
|
+
|
207
|
+
</div>
|
208
|
+
|
209
|
+
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
210
|
+
|
211
|
+
<ul class="nav navbar-nav navbar-right">
|
212
|
+
|
213
|
+
@if (Auth::check())
|
214
|
+
|
215
|
+
<li>{!! link_to_route('users.index', 'Users') !!}</li>
|
216
|
+
|
217
|
+
<li class="dropdown">
|
218
|
+
|
219
|
+
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ Auth::user()->name }} <span class="caret"></span></a>
|
220
|
+
|
221
|
+
<ul class="dropdown-menu">
|
222
|
+
|
223
|
+
<li>{!! link_to_route('users.show', 'My profile', ['id' => $user->id]) !!}</li>
|
224
|
+
|
225
|
+
<li>{!! link_to_route('micropost.favorites', 'Favorites') !!}</li> <!--TODO-->
|
226
|
+
|
227
|
+
<li role="separator" class="divider"></li>
|
228
|
+
|
229
|
+
<li>{!! link_to_route('logout.get', 'Logout') !!}</li>
|
230
|
+
|
231
|
+
</ul>
|
232
|
+
|
233
|
+
</li>
|
234
|
+
|
235
|
+
@else
|
236
|
+
|
237
|
+
<li>{!! link_to_route('signup.get', 'Signup') !!}</li>
|
238
|
+
|
239
|
+
<li>{!! link_to_route('login.get', 'Login') !!}</li>
|
240
|
+
|
241
|
+
@endif
|
242
|
+
|
243
|
+
</ul>
|
244
|
+
|
245
|
+
</div>
|
246
|
+
|
247
|
+
</div>
|
248
|
+
|
249
|
+
</nav>
|
250
250
|
|
251
251
|
</header>
|
252
252
|
|
@@ -284,160 +284,160 @@
|
|
284
284
|
|
285
285
|
{
|
286
286
|
|
287
|
-
public function index()
|
287
|
+
public function index()
|
288
|
-
|
288
|
+
|
289
|
-
{
|
289
|
+
{
|
290
|
-
|
290
|
+
|
291
|
-
$users = User::paginate(10);
|
291
|
+
$users = User::paginate(10);
|
292
|
-
|
293
|
-
|
294
|
-
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
-
return view('users.index', [
|
295
|
+
return view('users.index', [
|
296
|
-
|
296
|
+
|
297
|
-
'users' => $users,
|
297
|
+
'users' => $users,
|
298
|
-
|
298
|
+
|
299
|
-
]);
|
299
|
+
]);
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
public function show($id)
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
$user = User::find($id);
|
310
|
+
|
311
|
+
$microposts = $user->microposts()->orderBy('created_at', 'DESC')->paginate(10);
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
$data = [
|
316
|
+
|
317
|
+
'user' => $user,
|
318
|
+
|
319
|
+
'microposts' => $microposts,
|
320
|
+
|
321
|
+
];
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
$data += $this->counts($user);
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
return view('users.show', $data);
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
public function followings($id)
|
336
|
+
|
337
|
+
{
|
338
|
+
|
339
|
+
$user = User::find($id);
|
340
|
+
|
341
|
+
$followings = $user->followings()->paginate(10);
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
$data = [
|
346
|
+
|
347
|
+
'user' => $user,
|
348
|
+
|
349
|
+
'users' => $followings,
|
350
|
+
|
351
|
+
];
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
$data += $this->counts($user);
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
return view('users.followings', $data);
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
public function followers($id)
|
366
|
+
|
367
|
+
{
|
368
|
+
|
369
|
+
$user = User::find($id);
|
370
|
+
|
371
|
+
$followers = $user->followers()->paginate(10);
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
$data = [
|
376
|
+
|
377
|
+
'user' => $user,
|
378
|
+
|
379
|
+
'users' => $followers,
|
380
|
+
|
381
|
+
];
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
$data += $this->counts($user);
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
return view('users.followers', $data);
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
public function favorites($id)
|
396
|
+
|
397
|
+
{
|
398
|
+
|
399
|
+
$user = User::find($id);
|
400
|
+
|
401
|
+
$microposts = $user->microposts()->orderBy('created_at', 'desc')->paginate(10);
|
402
|
+
|
403
|
+
$favorites = $user->favorites()->orderBy('created_at', 'desc')->paginate(10);
|
404
|
+
|
405
|
+
foreach ($microposts as $micropost) {
|
406
|
+
|
407
|
+
if ($micropost->id === $favorites->user_id) {
|
408
|
+
|
409
|
+
$favorite_microposts = $micropost;
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
$data = [
|
418
|
+
|
419
|
+
'user' => $user,
|
420
|
+
|
421
|
+
'microposts' => $microposts,
|
422
|
+
|
423
|
+
'favorites' => $favorites,
|
424
|
+
|
425
|
+
'favorite_microposts' => $favorite_microposts,
|
426
|
+
|
427
|
+
];
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
$data += $this->counts($user);
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
return view('users.favorites', $data);
|
436
|
+
|
437
|
+
}
|
300
438
|
|
301
439
|
}
|
302
440
|
|
303
|
-
|
304
|
-
|
305
|
-
public function show($id)
|
306
|
-
|
307
|
-
{
|
308
|
-
|
309
|
-
$user = User::find($id);
|
310
|
-
|
311
|
-
$microposts = $user->microposts()->orderBy('created_at', 'DESC')->paginate(10);
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
$data = [
|
316
|
-
|
317
|
-
'user' => $user,
|
318
|
-
|
319
|
-
'microposts' => $microposts,
|
320
|
-
|
321
|
-
];
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
$data += $this->counts($user);
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
return view('users.show', $data);
|
330
|
-
|
331
|
-
}
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
public function followings($id)
|
336
|
-
|
337
|
-
{
|
338
|
-
|
339
|
-
$user = User::find($id);
|
340
|
-
|
341
|
-
$followings = $user->followings()->paginate(10);
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
$data = [
|
346
|
-
|
347
|
-
'user' => $user,
|
348
|
-
|
349
|
-
'users' => $followings,
|
350
|
-
|
351
|
-
];
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
$data += $this->counts($user);
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
return view('users.followings', $data);
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
public function followers($id)
|
366
|
-
|
367
|
-
{
|
368
|
-
|
369
|
-
$user = User::find($id);
|
370
|
-
|
371
|
-
$followers = $user->followers()->paginate(10);
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
$data = [
|
376
|
-
|
377
|
-
'user' => $user,
|
378
|
-
|
379
|
-
'users' => $followers,
|
380
|
-
|
381
|
-
];
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
$data += $this->counts($user);
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
return view('users.followers', $data);
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
public function favorites($id)
|
396
|
-
|
397
|
-
{
|
398
|
-
|
399
|
-
$user = User::find($id);
|
400
|
-
|
401
|
-
$microposts = $user->microposts()->orderBy('created_at', 'desc')->paginate(10);
|
402
|
-
|
403
|
-
$favorites = $user->favorites()->orderBy('created_at', 'desc')->paginate(10);
|
404
|
-
|
405
|
-
foreach ($microposts as $micropost) {
|
406
|
-
|
407
|
-
if ($micropost->id === $favorites->user_id) {
|
408
|
-
|
409
|
-
$favorite_microposts = $micropost;
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
$data = [
|
418
|
-
|
419
|
-
'user' => $user,
|
420
|
-
|
421
|
-
'microposts' => $microposts,
|
422
|
-
|
423
|
-
'favorites' => $favorites,
|
424
|
-
|
425
|
-
'favorite_microposts' => $favorite_microposts,
|
426
|
-
|
427
|
-
];
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
$data += $this->counts($user);
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
return view('users.favorites', $data);
|
436
|
-
|
437
|
-
}
|
438
|
-
|
439
|
-
}
|
440
|
-
|
441
441
|
```
|
442
442
|
|
443
443
|
|
@@ -448,6 +448,8 @@
|
|
448
448
|
|
449
449
|
<?php
|
450
450
|
|
451
|
+
|
452
|
+
|
451
453
|
Route::get('/', 'WelcomeController@index');
|
452
454
|
|
453
455
|
|
@@ -474,34 +476,34 @@
|
|
474
476
|
|
475
477
|
Route::group(['middleware' => 'auth'], function () {
|
476
478
|
|
477
|
-
Route::resource('users', 'UsersController', ['only' => ['index', 'show']]); // ユーザ一覧, ユーザ詳細
|
479
|
+
Route::resource('users', 'UsersController', ['only' => ['index', 'show']]); // ユーザ一覧, ユーザ詳細
|
478
|
-
|
480
|
+
|
479
|
-
Route::group(['prefix' => 'users/{id}'], function () {
|
481
|
+
Route::group(['prefix' => 'users/{id}'], function () {
|
480
|
-
|
482
|
+
|
481
|
-
Route::post('follow', 'UserFollowController@store')->name('user.follow');
|
483
|
+
Route::post('follow', 'UserFollowController@store')->name('user.follow');
|
482
|
-
|
484
|
+
|
483
|
-
Route::delete('unfollow', 'UserFollowController@destroy')->name('user.unfollow');
|
485
|
+
Route::delete('unfollow', 'UserFollowController@destroy')->name('user.unfollow');
|
484
|
-
|
486
|
+
|
485
|
-
Route::get('followings', 'UsersController@followings')->name('users.followings');
|
487
|
+
Route::get('followings', 'UsersController@followings')->name('users.followings');
|
486
|
-
|
488
|
+
|
487
|
-
Route::get('followers', 'UsersController@followers')->name('users.followers');
|
489
|
+
Route::get('followers', 'UsersController@followers')->name('users.followers');
|
490
|
+
|
491
|
+
});
|
492
|
+
|
493
|
+
Route::resource('microposts', 'MicropostsController', ['only' => ['store', 'destroy']]); // 投稿の保存, 投稿の削除
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
// お気に入り
|
498
|
+
|
499
|
+
Route::post('favorite/{id}', 'FavoritesController@store')->name('micropost.favorite');
|
500
|
+
|
501
|
+
Route::delete('unfavorite/{id}', 'FavoritesController@destroy')->name('micropost.unfavorite');
|
502
|
+
|
503
|
+
Route::get('favorites/{id}', 'UsersController@favorites')->name('micropost.favorites');
|
488
504
|
|
489
505
|
});
|
490
506
|
|
491
|
-
Route::resource('microposts', 'MicropostsController', ['only' => ['store', 'destroy']]); // 投稿の保存, 投稿の削除
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
// お気に入り
|
496
|
-
|
497
|
-
Route::post('favorite/{id}', 'FavoritesController@store')->name('micropost.favorite');
|
498
|
-
|
499
|
-
Route::delete('unfavorite/{id}', 'FavoritesController@destroy')->name('micropost.unfavorite');
|
500
|
-
|
501
|
-
Route::get('favorites/{id}', 'UsersController@favorites')->name('micropost.favorites');
|
502
|
-
|
503
|
-
});
|
504
|
-
|
505
507
|
```
|
506
508
|
|
507
509
|
|
2
「試したこと」を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,6 +124,50 @@
|
|
124
124
|
|
125
125
|
|
126
126
|
|
127
|
+
### 試したこと
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
__UsersController.php__ の `show(){}` の様に、
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
public function index($id)
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
$users = User::paginate(10);
|
140
|
+
|
141
|
+
$user = User::find($id);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
return view('users.index', [
|
146
|
+
|
147
|
+
'users' => $users,
|
148
|
+
|
149
|
+
'user' => $user,
|
150
|
+
|
151
|
+
]);
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
とすれば __index.blade.php__ にログインユーザーの情報を渡すことができるのではないかと思ったのですが、それをすると Missing Argument エラーが出てしまいます。
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
1/1
|
162
|
+
|
163
|
+
ErrorException in UsersController.php line 14:
|
164
|
+
|
165
|
+
Missing argument 1 for App\Http\Controllers\UsersController::index()
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
|
170
|
+
|
127
171
|
### 該当のソースコード
|
128
172
|
|
129
173
|
|
1
「ヒントになりそうなこと」を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,6 +92,38 @@
|
|
92
92
|
|
93
93
|
|
94
94
|
|
95
|
+
### ヒントになりそうなこと
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
__UsersController.php__ の `index()` に以下を追記すると1.と2.を両方とも実現することができます。
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
public function index()
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
$users = User::paginate(10);
|
108
|
+
|
109
|
+
$user = User::find(2); // 追記。 動作確認としてidを直接指定。$userにログインユーザーのidを代入することができれば実現可能?
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
return view('users.index', [
|
114
|
+
|
115
|
+
'users' => $users,
|
116
|
+
|
117
|
+
'user' => $user, // 追記。ユーザー一覧ページのviewに渡す
|
118
|
+
|
119
|
+
]);
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
|
126
|
+
|
95
127
|
### 該当のソースコード
|
96
128
|
|
97
129
|
|