質問編集履歴
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -210,80 +210,444 @@
|
|
210
210
|
|
211
211
|
at TransformsRequest->handle()
|
212
212
|
|
213
|
-
|
213
|
+
|
214
|
-
|
215
|
-
at Pipeline->Illuminate\Pipeline\{closure}()
|
216
|
-
|
217
|
-
in Pipeline.php line 53
|
218
|
-
|
219
|
-
at Pipeline->Illuminate\Routing\{closure}()
|
220
|
-
|
221
|
-
in TransformsRequest.php line 21
|
222
|
-
|
223
|
-
at TransformsRequest->handle()
|
224
|
-
|
225
|
-
in Pipeline.php line 163
|
226
|
-
|
227
|
-
at Pipeline->Illuminate\Pipeline\{closure}()
|
228
|
-
|
229
|
-
in Pipeline.php line 53
|
230
|
-
|
231
|
-
at Pipeline->Illuminate\Routing\{closure}()
|
232
|
-
|
233
|
-
in ValidatePostSize.php line 27
|
234
|
-
|
235
|
-
at ValidatePostSize->handle()
|
236
|
-
|
237
|
-
in Pipeline.php line 163
|
238
|
-
|
239
|
-
at Pipeline->Illuminate\Pipeline\{closure}()
|
240
|
-
|
241
|
-
in Pipeline.php line 53
|
242
|
-
|
243
|
-
at Pipeline->Illuminate\Routing\{closure}()
|
244
|
-
|
245
|
-
in CheckForMaintenanceMode.php line 62
|
246
|
-
|
247
|
-
at CheckForMaintenanceMode->handle()
|
248
|
-
|
249
|
-
in Pipeline.php line 163
|
250
|
-
|
251
|
-
at Pipeline->Illuminate\Pipeline\{closure}()
|
252
|
-
|
253
|
-
in Pipeline.php line 53
|
254
|
-
|
255
|
-
at Pipeline->Illuminate\Routing\{closure}()
|
256
|
-
|
257
|
-
in TrustProxies.php line 57
|
258
|
-
|
259
|
-
at TrustProxies->handle()
|
260
|
-
|
261
|
-
in Pipeline.php line 163
|
262
|
-
|
263
|
-
at Pipeline->Illuminate\Pipeline\{closure}()
|
264
|
-
|
265
|
-
in Pipeline.php line 53
|
266
|
-
|
267
|
-
at Pipeline->Illuminate\Routing\{closure}()
|
268
|
-
|
269
|
-
in Pipeline.php line 104
|
270
|
-
|
271
|
-
at Pipeline->then()
|
272
|
-
|
273
|
-
in Kernel.php line 151
|
274
|
-
|
275
|
-
at Kernel->sendRequestThroughRouter()
|
276
|
-
|
277
|
-
in Kernel.php line 116
|
278
|
-
|
279
|
-
at Kernel->handle()
|
280
|
-
|
281
|
-
in index.php line 55
|
282
214
|
|
283
215
|
```
|
284
216
|
|
285
217
|
|
286
218
|
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
UserController.php
|
228
|
+
|
229
|
+
```ここに言語を入力
|
230
|
+
|
231
|
+
<?php
|
232
|
+
|
233
|
+
namespace App\Http\Controllers;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
use Illuminate\Support\Facades\Mail;
|
238
|
+
|
239
|
+
use Illuminate\Validation\Rule;
|
240
|
+
|
241
|
+
use Illuminate\Support\Facades\DB;
|
242
|
+
|
243
|
+
use Illuminate\Support\Facades\Storage;
|
244
|
+
|
245
|
+
use Illuminate\Support\Facades\Log;
|
246
|
+
|
247
|
+
use Illuminate\Http\Request;
|
248
|
+
|
249
|
+
use Illuminate\Support\Facades\Auth;
|
250
|
+
|
251
|
+
use Illuminate\Support\Str;
|
252
|
+
|
253
|
+
use Illuminate\Support\Facades\Hash;
|
254
|
+
|
255
|
+
use phpDocumentor\Reflection\Types\Boolean;
|
256
|
+
|
257
|
+
use Carbon\Carbon;
|
258
|
+
|
259
|
+
use App\User;
|
260
|
+
|
261
|
+
use App\Buyer;
|
262
|
+
|
263
|
+
use App\Product;
|
264
|
+
|
265
|
+
use App\Order;
|
266
|
+
|
267
|
+
use App\EmailReset;
|
268
|
+
|
269
|
+
use App\Rules\Hankaku;
|
270
|
+
|
271
|
+
use App\Mail\ChangeEmail;
|
272
|
+
|
273
|
+
use App\Http\Requests\UserProfileRequest;
|
274
|
+
|
275
|
+
use App\Http\Requests\ProductRequest;
|
276
|
+
|
277
|
+
use Request as PostRequest;
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
class UserController extends Controller {
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
//-----------------------------------------------------------
|
286
|
+
|
287
|
+
//ユーザが投稿した最近の記事5件
|
288
|
+
|
289
|
+
//-----------------------------------------------------------
|
290
|
+
|
291
|
+
public function index(int $id){
|
292
|
+
|
293
|
+
$user = User::where('id', $id)->first();
|
294
|
+
|
295
|
+
$is_img = $this->isImg();
|
296
|
+
|
297
|
+
$products = $user->products->take(5);
|
298
|
+
|
299
|
+
//ユーザーと商品のリレーションから、最新の5件の商品を取得
|
300
|
+
|
301
|
+
$soldOutProducts = $user->soldProduct;
|
302
|
+
|
303
|
+
//hasmanythroughでusers->products->ordersテーブルをまたいでアクセス。売れた商品を取得
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
if (!empty($soldOutProducts)) {
|
308
|
+
|
309
|
+
foreach ($soldOutProducts as $soldOutProduct) {
|
310
|
+
|
311
|
+
$product_id[] = $soldOutProduct->product_id;
|
312
|
+
|
313
|
+
}//ordersテーブルのproduct_idカラムを全て取得
|
314
|
+
|
315
|
+
if (!empty($product_id)) {
|
316
|
+
|
317
|
+
$soldOutProducts = Product::whereIn('id', $product_id)->get()->take(5);
|
318
|
+
|
319
|
+
//productsテーブルのからproduct_id[]の値を元に取得
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
return view('users.index', [
|
326
|
+
|
327
|
+
'products' => $products,
|
328
|
+
|
329
|
+
'user' => $user,
|
330
|
+
|
331
|
+
'soldOutProducts' => $soldOutProducts,
|
332
|
+
|
333
|
+
]);
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
//----------------------------------------
|
340
|
+
|
341
|
+
//出品中の商品
|
342
|
+
|
343
|
+
//----------------------------------------
|
344
|
+
|
345
|
+
public function onSellProduct(int $id){
|
346
|
+
|
347
|
+
$user = User::where('id', $id)->first();
|
348
|
+
|
349
|
+
//$idをもとにuser情報取得
|
350
|
+
|
351
|
+
$products = $user->products()->paginate(10);
|
352
|
+
|
353
|
+
//user情報をもとに商品を取得
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
return view('users.onSellProducts', [
|
358
|
+
|
359
|
+
'user' => $user,
|
360
|
+
|
361
|
+
'products' => $products,
|
362
|
+
|
363
|
+
]);
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
//----------------------------------------
|
368
|
+
|
369
|
+
//売れた商品
|
370
|
+
|
371
|
+
//----------------------------------------
|
372
|
+
|
373
|
+
public function soldProduct($id) {
|
374
|
+
|
375
|
+
$user = User::where('id', $id)->first();
|
376
|
+
|
377
|
+
//ユーザー情報
|
378
|
+
|
379
|
+
$soldOutProducts = $user->soldProduct;
|
380
|
+
|
381
|
+
//hasmanythroughでusers->products->ordersテーブルにアクセス。
|
382
|
+
|
383
|
+
//ユーザーが出品した商品で売れたものをとる
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
if (!empty($soldOutProducts)) {
|
388
|
+
|
389
|
+
foreach ($soldOutProducts as $soldOutProduct) {
|
390
|
+
|
391
|
+
$product_id[] = $soldOutProduct->product_id;
|
392
|
+
|
393
|
+
Log::debug(print_r( $product_id, true));
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
if (!empty($product_id)) {
|
398
|
+
|
399
|
+
//ordersテーブルから売れた商品のproduct_idを取得
|
400
|
+
|
401
|
+
$soldOutProducts = Product::whereIn('id',$product_id)->paginate(10);
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
return view('users.soldProducts', [
|
408
|
+
|
409
|
+
'soldOutProducts' => $soldOutProducts,
|
410
|
+
|
411
|
+
'user' => $user
|
412
|
+
|
413
|
+
]);
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
//----------------------------------------
|
426
|
+
|
427
|
+
//ユーザープロフィール編集
|
428
|
+
|
429
|
+
//----------------------------------------
|
430
|
+
|
431
|
+
public function profile(int $id){
|
432
|
+
|
433
|
+
$user = User::where('id', $id)->first();
|
434
|
+
|
435
|
+
$prefs = config('prefectures');
|
436
|
+
|
437
|
+
$is_img = $this->isImg();
|
438
|
+
|
439
|
+
Log::debug(print_r($is_img, true));
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
return view('users.profile', [
|
444
|
+
|
445
|
+
'user' => $user,
|
446
|
+
|
447
|
+
'prefs' => $prefs,
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
]);
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
//----------------------------------------
|
458
|
+
|
459
|
+
//ユーザープロフィール編集
|
460
|
+
|
461
|
+
//----------------------------------------
|
462
|
+
|
463
|
+
// GETパラメータが数字かどうかをチェックする
|
464
|
+
|
465
|
+
public function update(UserProfileRequest $request, int $id) {
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
$user = User::where('id', $id)->first();
|
472
|
+
|
473
|
+
$user->shop_name = $request->shop_name;
|
474
|
+
|
475
|
+
$user->branch_name = $request->branch_name;
|
476
|
+
|
477
|
+
$user->prefecture = $request->prefecture ;
|
478
|
+
|
479
|
+
$user->address = $request->address;
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
if (!empty($request->img)) {
|
484
|
+
|
485
|
+
Storage::delete('public/userProfile_images', Auth::id() . '.jpg');
|
486
|
+
|
487
|
+
Log::debug(print_r($request->img, true));
|
488
|
+
|
489
|
+
$user->img = $request->file('img')->storeAs('public/userProfile_images', Auth::id() . '.jpg');
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
$user->save();
|
496
|
+
|
497
|
+
return back()->with('flash_message', '変更が完了しました');
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
```
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
Product.php
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
```ここに言語を入力
|
516
|
+
|
517
|
+
<?php
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
namespace App;
|
522
|
+
|
523
|
+
use Illuminate\Support\Facades\Auth;
|
524
|
+
|
525
|
+
use Illuminate\Support\Facades\Log;
|
526
|
+
|
527
|
+
use Illuminate\Database\Eloquent\Relations\HasMany;
|
528
|
+
|
529
|
+
use Illuminate\Database\Eloquent\Relations\HasOne;
|
530
|
+
|
531
|
+
use Illuminate\Database\Eloquent\Model;
|
532
|
+
|
533
|
+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
class Product extends Model
|
542
|
+
|
543
|
+
{
|
544
|
+
|
545
|
+
protected $fillable = ['product_name','expiration_date','price','img',];
|
546
|
+
|
547
|
+
//----------------------------
|
548
|
+
|
549
|
+
//userとのリレーション(1対多)
|
550
|
+
|
551
|
+
//----------------------------
|
552
|
+
|
553
|
+
public function user(): BelongsTo//このメソッドの戻り値の「型」を宣言
|
554
|
+
|
555
|
+
{
|
556
|
+
|
557
|
+
return $this->belongsTo('App\User');
|
558
|
+
|
559
|
+
}
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
//----------------------------
|
564
|
+
|
565
|
+
//buyerとのリレーション(多対多)
|
566
|
+
|
567
|
+
//product->ordersで自分を買ったバイヤーの情報取得
|
568
|
+
|
569
|
+
//product->orders()->attachで中間テーブル更新
|
570
|
+
|
571
|
+
//----------------------------
|
572
|
+
|
573
|
+
public function orderBuyer(): BelongsToMany
|
574
|
+
|
575
|
+
{
|
576
|
+
|
577
|
+
return $this->belongsToMany('App\Buyer', 'orders','product_id', 'buyer_id')->withTimestamps();
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
}//第二引数には中間テーブルのテーブル名
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
//----------------------------
|
586
|
+
|
587
|
+
//ログインしているバイヤーがその商品を購入済みかの確認
|
588
|
+
|
589
|
+
//----------------------------
|
590
|
+
|
591
|
+
public function isOrderedBy(buyer $buyer): bool {
|
592
|
+
|
593
|
+
//viewから受け取った情報がbuyerモデルであることを確認
|
594
|
+
|
595
|
+
//返り値がboolであることを確認
|
596
|
+
|
597
|
+
// $a = $this->orders;
|
598
|
+
|
599
|
+
return $buyer
|
600
|
+
|
601
|
+
?(bool)$this->orderBuyer->where('id', $buyer->id)->count()
|
602
|
+
|
603
|
+
:false;
|
604
|
+
|
605
|
+
//buyerモデルを参照して中間テーブルにアクセス。
|
606
|
+
|
607
|
+
//ログインしているバイヤーのidをもとにordersテーブルから
|
608
|
+
|
609
|
+
//product->buyer->orders->>where('id', $buyer->id)->count()
|
610
|
+
|
611
|
+
//buyerモデルのコレクションが返る。
|
612
|
+
|
613
|
+
//thisはviewから送られてきたproduct
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
// $order = $buyer->buyers()->where('product_id', $id)->count();
|
618
|
+
|
619
|
+
//$buyer->buyers()でbuyerモデルからordersテーブル経由で紐付いているproductモデルのコレクションが返る
|
620
|
+
|
621
|
+
}
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
public function hasOrders(): HasOne//OK
|
626
|
+
|
627
|
+
{
|
628
|
+
|
629
|
+
return $this->hasOne('App\Order');
|
630
|
+
|
631
|
+
}
|
632
|
+
|
633
|
+
|
634
|
+
|
635
|
+
}
|
636
|
+
|
637
|
+
```
|
638
|
+
|
639
|
+
|
640
|
+
|
641
|
+
|
642
|
+
|
643
|
+
```
|
644
|
+
|
645
|
+
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
|
650
|
+
|
287
651
|
### 環境
|
288
652
|
|
289
653
|
mysql5.5
|