質問編集履歴

3

現状とimageカラムについて、情報を追記

2020/05/30 15:12

投稿

youth_k0228
youth_k0228

スコア18

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- 1.投稿した画像ファイルをstorage/app/imagesフォルダに保存
11
+ 1.投稿した画像ファイルをstorage/app/imagesフォルダに保存(現状出来上がったのはここまでです。)
12
12
 
13
13
  2.保存した画像ファイルのファイルパスをDBに格納
14
14
 
@@ -644,7 +644,7 @@
644
644
 
645
645
  以下、作成したDBのキャプチャです。
646
646
 
647
- 「image」カラム(String型)にファイルパスを格納するつもりです。
647
+ 「image」カラム(String型)にファイルパスを格納するつもりです。(スクショ撮影時はnullになっています。)
648
648
 
649
649
  ![![イメージ説明](42aa0b69d45a4d9ccc81f880ad2e3240.png)](e9281ac8a2e1f3bbb954bb301779ad60.png)
650
650
 

2

Pictureのモデルクラスを追記

2020/05/30 15:12

投稿

youth_k0228
youth_k0228

スコア18

test CHANGED
File without changes
test CHANGED
@@ -650,4 +650,36 @@
650
650
 
651
651
 
652
652
 
653
+ 20200530追記
654
+
655
+ Pictureのモデルクラスですが、artisanでファイルを作成したのみで、中身は未設定です。
656
+
657
+
658
+
659
+ ```picture.php
660
+
661
+ <?php
662
+
663
+
664
+
665
+ namespace App;
666
+
667
+
668
+
669
+ use Illuminate\Database\Eloquent\Model;
670
+
671
+
672
+
673
+ class picture extends Model
674
+
675
+ {
676
+
677
+ //
678
+
679
+ }
680
+
681
+
682
+
683
+ ```
684
+
653
685
  以上、不勉強で至らぬ所ではございますが、ご教授・ご助言をお願い致します。m(_ _)m

1

投稿内容を保存するクラス→Pictureクラス全体、各マイグレーションファイルの追加

2020/05/29 16:00

投稿

youth_k0228
youth_k0228

スコア18

test CHANGED
File without changes
test CHANGED
@@ -26,12 +26,92 @@
26
26
 
27
27
 
28
28
 
29
- 以下、投稿内容を保存するコントローラ、詳細ページのビュー、新規投稿ページのビュー、ルーティングのソースコードです。
29
+ 以下、Pictureクラスのコントローラ、詳細ページのビュー、新規投稿ページのビュー、ルーティング、picturesテーブル定義のソースコードです。
30
30
 
31
31
 
32
32
 
33
33
  ```PictureController.php
34
34
 
35
+ <?php
36
+
37
+
38
+
39
+ namespace App\Http\Controllers;
40
+
41
+
42
+
43
+ use Illuminate\Http\Request;
44
+
45
+ use App\Picture;
46
+
47
+
48
+
49
+ class PictureController extends Controller
50
+
51
+ {
52
+
53
+ /**
54
+
55
+ * Display a listing of the resource.
56
+
57
+ *
58
+
59
+ * @return \Illuminate\Http\Response
60
+
61
+ */
62
+
63
+ public function index(Request $request)
64
+
65
+ {
66
+
67
+ if($request->filled('keyword')){
68
+
69
+ $keyword=$request->input('keyword');
70
+
71
+ $message='「'.$keyword.'」で検索';
72
+
73
+ $pictures = Picture::where('content','like','%'.$keyword.'%')->get();
74
+
75
+ //データベースに格納されてあるデータの中で、入力されたキーワードが含まれているものを呼び出す。
76
+
77
+ }else{
78
+
79
+ $message='This is my picture memo.';
80
+
81
+ $pictures = Picture::all();
82
+
83
+ //データベースに格納されてあるデータを全て呼び出す。
84
+
85
+ }
86
+
87
+ return view('index',['message'=>$message,'pictures'=>$pictures]);
88
+
89
+ //html上にここで格納した変数の中身を表示させる為に渡す。
90
+
91
+ }
92
+
93
+
94
+
95
+ /**
96
+
97
+ * Show the form for creating a new resource.
98
+
99
+ *
100
+
101
+ * @return \Illuminate\Http\Response
102
+
103
+ */
104
+
105
+ public function create(Request $request)
106
+
107
+ {
108
+
109
+ $message='New picture';
110
+
111
+ return view('new',['message'=>$message]);
112
+
113
+ }
114
+
35
115
 
36
116
 
37
117
  /**
@@ -70,6 +150,124 @@
70
150
 
71
151
 
72
152
 
153
+ /**
154
+
155
+ * Display the specified resource.
156
+
157
+ *
158
+
159
+ * @param \App\picture $picture
160
+
161
+ * @return \Illuminate\Http\Response
162
+
163
+ */
164
+
165
+ public function show(Request $request,$id,Picture $picture)
166
+
167
+ {
168
+
169
+ $message='This is your picture.'.$id;
170
+
171
+ $picture=Picture::find($id);
172
+
173
+ //$idに格納された番号と一致したデータを引っ張り出す。
174
+
175
+ return view('show',['message'=>$message,'picture'=>$picture]);
176
+
177
+ }
178
+
179
+
180
+
181
+ /**
182
+
183
+ * Show the form for editing the specified resource.
184
+
185
+ *
186
+
187
+ * @param \App\picture $picture
188
+
189
+ * @return \Illuminate\Http\Response
190
+
191
+ */
192
+
193
+ public function edit(Request $request,$id,picture $picture)
194
+
195
+ {
196
+
197
+ $message='Edit your picture.'.$id;
198
+
199
+ $picture=Picture::find($id);
200
+
201
+ //$idに格納された番号と一致したデータを引っ張り出す。
202
+
203
+ return view('edit',['message'=>$message,'picture'=>$picture]);
204
+
205
+ }
206
+
207
+
208
+
209
+ /**
210
+
211
+ * Update the specified resource in storage.
212
+
213
+ *
214
+
215
+ * @param \Illuminate\Http\Request $request
216
+
217
+ * @param \App\picture $picture
218
+
219
+ * @return \Illuminate\Http\Response
220
+
221
+ */
222
+
223
+ public function update(Request $request,$id, picture $picture)
224
+
225
+ {
226
+
227
+ $picture=Picture::find($id);
228
+
229
+ //登録したブックマークをDBに格納させる
230
+
231
+ $picture->content=$request->content;
232
+
233
+ $picture->user_name=$request->user_name;
234
+
235
+ $picture->save();
236
+
237
+ //tinkerコマンドと同じ
238
+
239
+ return redirect()->route('picture.show',['id'=>$picture->id]);
240
+
241
+ }
242
+
243
+
244
+
245
+ /**
246
+
247
+ * Remove the specified resource from storage.
248
+
249
+ *
250
+
251
+ * @param \App\picture $picture
252
+
253
+ * @return \Illuminate\Http\Response
254
+
255
+ */
256
+
257
+ public function destroy(Request $request,$id,picture $picture)
258
+
259
+ {
260
+
261
+ $picture=Picture::find($id);
262
+
263
+ $picture->delete();
264
+
265
+ return redirect('/pictures');
266
+
267
+ }
268
+
269
+ }
270
+
73
271
  ```
74
272
 
75
273
 
@@ -228,11 +426,227 @@
228
426
 
229
427
  ```
230
428
 
429
+ ```2020_01_05_062834_create_pictures_table.php
430
+
431
+ <?php
432
+
433
+
434
+
435
+ use Illuminate\Database\Migrations\Migration;
436
+
437
+ use Illuminate\Database\Schema\Blueprint;
438
+
439
+ use Illuminate\Support\Facades\Schema;
440
+
441
+
442
+
443
+ class CreatepicturesTable extends Migration
444
+
445
+ {
446
+
447
+ /**
448
+
449
+ * Run the migrations.
450
+
451
+ *
452
+
453
+ * @return void
454
+
455
+ */
456
+
457
+ public function up()
458
+
459
+ {
460
+
461
+ Schema::create('pictures', function (Blueprint $table) {
462
+
463
+ $table->bigIncrements('id');
464
+
465
+ $table->string('content');
466
+
467
+ $table->timestamps();
468
+
469
+ });
470
+
471
+ }
472
+
473
+
474
+
475
+ /**
476
+
477
+ * Reverse the migrations.
478
+
479
+ *
480
+
481
+ * @return void
482
+
483
+ */
484
+
485
+ public function down()
486
+
487
+ {
488
+
489
+ Schema::dropIfExists('pictures');
490
+
491
+ }
492
+
493
+ }
494
+
495
+
496
+
497
+ ```
498
+
499
+
500
+
501
+ ```2020_01_12_155013_add_column_username.php
502
+
503
+ <?php
504
+
505
+
506
+
507
+ use Illuminate\Database\Migrations\Migration;
508
+
509
+ use Illuminate\Database\Schema\Blueprint;
510
+
511
+ use Illuminate\Support\Facades\Schema;
512
+
513
+
514
+
515
+ class AddColumnUsername extends Migration
516
+
517
+ {
518
+
519
+ /**
520
+
521
+ * Run the migrations.
522
+
523
+ *
524
+
525
+ * @return void
526
+
527
+ */
528
+
529
+ public function up()
530
+
531
+ {
532
+
533
+ Schema::table('pictures', function (Blueprint $table) {
534
+
535
+ $table->string('user_name');
536
+
537
+ });
538
+
539
+ }
540
+
541
+
542
+
543
+ /**
544
+
545
+ * Reverse the migrations.
546
+
547
+ *
548
+
549
+ * @return void
550
+
551
+ */
552
+
553
+ public function down()
554
+
555
+ {
556
+
557
+ Schema::table('picture', function (Blueprint $table) {
558
+
559
+ //
560
+
561
+ });
562
+
563
+ }
564
+
565
+ }
566
+
567
+
568
+
569
+ ```
570
+
571
+ ```2020_05_27_145554_add_column_image.php
572
+
573
+ <?php
574
+
575
+
576
+
577
+ use Illuminate\Database\Migrations\Migration;
578
+
579
+ use Illuminate\Database\Schema\Blueprint;
580
+
581
+ use Illuminate\Support\Facades\Schema;
582
+
583
+
584
+
585
+ class AddColumnImage extends Migration
586
+
587
+ {
588
+
589
+ /**
590
+
591
+ * Run the migrations.
592
+
593
+ *
594
+
595
+ * @return void
596
+
597
+ */
598
+
599
+ public function up()
600
+
601
+ {
602
+
603
+ Schema::table('pictures', function (Blueprint $table) {
604
+
605
+ //投稿した画像のファイルパスを格納
606
+
607
+ $table->string('image');
608
+
609
+ });
610
+
611
+ }
612
+
613
+
614
+
615
+ /**
616
+
617
+ * Reverse the migrations.
618
+
619
+ *
620
+
621
+ * @return void
622
+
623
+ */
624
+
625
+ public function down()
626
+
627
+ {
628
+
629
+ Schema::table('pictures', function (Blueprint $table) {
630
+
631
+ //
632
+
633
+ });
634
+
635
+ }
636
+
637
+ }
638
+
639
+
640
+
641
+ ```
642
+
643
+
644
+
231
645
  以下、作成したDBのキャプチャです。
232
646
 
233
647
  「image」カラム(String型)にファイルパスを格納するつもりです。
234
648
 
235
- ![![イメージ説明](42aa0b69d45a4d9ccc81f880ad2e3240.png)](e9281ac8a2e1f3bbb954bb301779ad60.png)
649
+ ![![イメージ説明](42aa0b69d45a4d9ccc81f880ad2e3240.png)](e9281ac8a2e1f3bbb954bb301779ad60.png)
236
650
 
237
651
 
238
652