質問編集履歴

3

ソースコードの編集

2020/04/08 06:22

投稿

neomana-39
neomana-39

スコア11

test CHANGED
File without changes
test CHANGED
@@ -160,7 +160,7 @@
160
160
 
161
161
  ```
162
162
 
163
- app/Http/Controller/ProfileController
163
+ app/Http/Controllers/ProfileController
164
164
 
165
165
  ```
166
166
 
@@ -454,6 +454,254 @@
454
454
 
455
455
  ```
456
456
 
457
+ app/Http/Controllers/StoriesController.php
458
+
459
+ ```
460
+
461
+ <?php
462
+
463
+
464
+
465
+ namespace App\Http\Controllers;
466
+
467
+
468
+
469
+ use Illuminate\Http\Request;
470
+
471
+ use App\Http\Controllers\Controller;
472
+
473
+ use App\Story;
474
+
475
+ use App\Profile;
476
+
477
+ use Auth;
478
+
479
+ use App\Posts;
480
+
481
+ use App\History;
482
+
483
+ use App\Attachment;
484
+
485
+ use Carbon\Carbon;
486
+
487
+ use Storage;
488
+
489
+
490
+
491
+ class StoriesController extends Controller
492
+
493
+ {
494
+
495
+
496
+
497
+ public function __construct()
498
+
499
+ {
500
+
501
+ $this->middleware('auth');
502
+
503
+ }
504
+
505
+
506
+
507
+ public function index(Request $request)
508
+
509
+ {
510
+
511
+ $images = Attachment::all();
512
+
513
+
514
+
515
+ $cond_title = $request->cond_title;
516
+
517
+ if ($cond_title != '') {
518
+
519
+ // 検索されたら検索結果を取得する
520
+
521
+ $posts = Profile::where('title', $cond_title)->get();
522
+
523
+ } else {
524
+
525
+ // それ以外はすべてのニュースを取得する
526
+
527
+ $posts = Profile::all();
528
+
529
+ }
530
+
531
+ return view('stories.index2', compact('images','posts','cond_title'));
532
+
533
+ }
534
+
535
+
536
+
537
+ public function add()
538
+
539
+ {
540
+
541
+ return view('stories.create2');
542
+
543
+ }
544
+
545
+
546
+
547
+
548
+
549
+ public function store(Request $request)
550
+
551
+ {
552
+
553
+ $d = new \DateTime();
554
+
555
+ $d->setTimeZone(new \DateTimeZone('Asia/Tokyo'));
556
+
557
+ $dir = $d->format('Y/m');
558
+
559
+ $path = sprintf('public/images/%s', $dir);
560
+
561
+
562
+
563
+
564
+
565
+ $data = $request->except('_token');
566
+
567
+
568
+
569
+ foreach ($data['images'] as $k => $v) {
570
+
571
+
572
+
573
+ $filename = '';
574
+
575
+
576
+
577
+
578
+
579
+ $attachments = Attachment::take(1)->orderBy('id', 'desc')->get();
580
+
581
+
582
+
583
+ foreach ($attachments as $attachment) {
584
+
585
+
586
+
587
+ $filename = $attachment->id + 1 . '_' . $v->getClientOriginalName();
588
+
589
+ }
590
+
591
+ unset($attachment);
592
+
593
+
594
+
595
+ if ($filename == false) {
596
+
597
+ $filename = 1 . '_' . $v->getClientOriginalName();
598
+
599
+ }
600
+
601
+
602
+
603
+ $v->storeAs($path, $filename);
604
+
605
+
606
+
607
+ $attachment_data = [
608
+
609
+ 'path' => sprintf('images/%s/', $dir),
610
+
611
+ 'name' => $filename
612
+
613
+ ];
614
+
615
+
616
+
617
+ $a = new Attachment();
618
+
619
+ $a->fill($attachment_data)->save();
620
+
621
+ }
622
+
623
+
624
+
625
+ unset($k, $v);
626
+
627
+
628
+
629
+ return redirect('/');
630
+
631
+ }
632
+
633
+
634
+
635
+
636
+
637
+ public function delete(Request $request)
638
+
639
+ {
640
+
641
+ // 該当するNews Modelを取得
642
+
643
+ $images = Attachment::find($request->id);
644
+
645
+ // 削除する
646
+
647
+ $images->delete();
648
+
649
+ return redirect('/');
650
+
651
+ }
652
+
653
+
654
+
655
+
656
+
657
+ public function upload(Request $request)
658
+
659
+ {
660
+
661
+ $this->validate($request, [
662
+
663
+ 'file' => [
664
+
665
+ 'required',
666
+
667
+ 'file',
668
+
669
+ 'image',
670
+
671
+ 'mimes:jpeg,png',
672
+
673
+ ]
674
+
675
+ ]);
676
+
677
+
678
+
679
+ if ($request->file('file')->isValid([])) {
680
+
681
+ $path = $request->file->store('public');
682
+
683
+ return view('stories.index2')->with('filename', basename($path));
684
+
685
+ } else {
686
+
687
+ return redirect('/')
688
+
689
+ ->back()
690
+
691
+ ->withInput()
692
+
693
+ ->withErrors();
694
+
695
+ }
696
+
697
+ }
698
+
699
+ }
700
+
701
+
702
+
703
+ ```
704
+
457
705
 
458
706
 
459
707
  ### 試したこと

2

追加情報の更新

2020/04/08 06:22

投稿

neomana-39
neomana-39

スコア11

test CHANGED
File without changes
test CHANGED
@@ -466,6 +466,8 @@
466
466
 
467
467
 
468
468
 
469
+ ![イメージ説明](60a8639ed99d95423b6fd83b19f4b7e3.png)
470
+
469
471
  よろしくお願いします。
470
472
 
471
473
 

1

ソースコードの編集

2020/04/08 04:50

投稿

neomana-39
neomana-39

スコア11

test CHANGED
File without changes
test CHANGED
@@ -96,6 +96,8 @@
96
96
 
97
97
  <a href="{{ action('ProfileController@delete', ['id' => $profile->id]) }}">delete</a>
98
98
 
99
+ <a href="{{ action('ProfileController@update', ['id' => $profile->id]) }}" class="update">update</a>
100
+
99
101
  @endforeach
100
102
 
101
103
 
@@ -104,8 +106,6 @@
104
106
 
105
107
  <br>
106
108
 
107
- <a href="{{ action('ProfileController@update', ['id' => $profile->id]) }}" class="update">update</a>
108
-
109
109
  </div>
110
110
 
111
111
  </div>