質問編集履歴
3
現状とimageカラムについて、情報を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
storage:link ArtisanコマンドでStorageフォルダを作成、その中に画像を保存するところまでは上手くできました。
|
4
4
|
具体的に行いたいことは下記の通りです。
|
5
5
|
|
6
|
-
1.投稿した画像ファイルをstorage/app/imagesフォルダに保存
|
6
|
+
1.投稿した画像ファイルをstorage/app/imagesフォルダに保存(現状出来上がったのはここまでです。)
|
7
7
|
2.保存した画像ファイルのファイルパスをDBに格納
|
8
8
|
3.ファイルパスを参照し、詳細ぺージで画像を表示
|
9
9
|
|
@@ -321,7 +321,7 @@
|
|
321
321
|
```
|
322
322
|
|
323
323
|
以下、作成したDBのキャプチャです。
|
324
|
-
「image」カラム(String型)にファイルパスを格納するつもりです。
|
324
|
+
「image」カラム(String型)にファイルパスを格納するつもりです。(スクショ撮影時はnullになっています。)
|
325
325
|
](e9281ac8a2e1f3bbb954bb301779ad60.png)
|
326
326
|
|
327
327
|
20200530追記
|
2
Pictureのモデルクラスを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -324,4 +324,20 @@
|
|
324
324
|
「image」カラム(String型)にファイルパスを格納するつもりです。
|
325
325
|
](e9281ac8a2e1f3bbb954bb301779ad60.png)
|
326
326
|
|
327
|
+
20200530追記
|
328
|
+
Pictureのモデルクラスですが、artisanでファイルを作成したのみで、中身は未設定です。
|
329
|
+
|
330
|
+
```picture.php
|
331
|
+
<?php
|
332
|
+
|
333
|
+
namespace App;
|
334
|
+
|
335
|
+
use Illuminate\Database\Eloquent\Model;
|
336
|
+
|
337
|
+
class picture extends Model
|
338
|
+
{
|
339
|
+
//
|
340
|
+
}
|
341
|
+
|
342
|
+
```
|
327
343
|
以上、不勉強で至らぬ所ではございますが、ご教授・ご助言をお願い致します。m(_ _)m
|
1
投稿内容を保存するクラス→Pictureクラス全体、各マイグレーションファイルの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,11 +12,51 @@
|
|
12
12
|
OS:Windows10 Home
|
13
13
|
DB:MySQL(phpmyadmin)
|
14
14
|
|
15
|
-
以下、
|
15
|
+
以下、Pictureクラスのコントローラ、詳細ページのビュー、新規投稿ページのビュー、ルーティング、picturesテーブル定義のソースコードです。
|
16
16
|
|
17
17
|
```PictureController.php
|
18
|
+
<?php
|
18
19
|
|
20
|
+
namespace App\Http\Controllers;
|
21
|
+
|
22
|
+
use Illuminate\Http\Request;
|
23
|
+
use App\Picture;
|
24
|
+
|
25
|
+
class PictureController extends Controller
|
26
|
+
{
|
19
27
|
/**
|
28
|
+
* Display a listing of the resource.
|
29
|
+
*
|
30
|
+
* @return \Illuminate\Http\Response
|
31
|
+
*/
|
32
|
+
public function index(Request $request)
|
33
|
+
{
|
34
|
+
if($request->filled('keyword')){
|
35
|
+
$keyword=$request->input('keyword');
|
36
|
+
$message='「'.$keyword.'」で検索';
|
37
|
+
$pictures = Picture::where('content','like','%'.$keyword.'%')->get();
|
38
|
+
//データベースに格納されてあるデータの中で、入力されたキーワードが含まれているものを呼び出す。
|
39
|
+
}else{
|
40
|
+
$message='This is my picture memo.';
|
41
|
+
$pictures = Picture::all();
|
42
|
+
//データベースに格納されてあるデータを全て呼び出す。
|
43
|
+
}
|
44
|
+
return view('index',['message'=>$message,'pictures'=>$pictures]);
|
45
|
+
//html上にここで格納した変数の中身を表示させる為に渡す。
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Show the form for creating a new resource.
|
50
|
+
*
|
51
|
+
* @return \Illuminate\Http\Response
|
52
|
+
*/
|
53
|
+
public function create(Request $request)
|
54
|
+
{
|
55
|
+
$message='New picture';
|
56
|
+
return view('new',['message'=>$message]);
|
57
|
+
}
|
58
|
+
|
59
|
+
/**
|
20
60
|
* Store a newly created resource in storage.
|
21
61
|
*
|
22
62
|
* @param \Illuminate\Http\Request $request
|
@@ -34,6 +74,65 @@
|
|
34
74
|
return redirect()->route('picture.show',['id'=>$picture->id]);
|
35
75
|
}
|
36
76
|
|
77
|
+
/**
|
78
|
+
* Display the specified resource.
|
79
|
+
*
|
80
|
+
* @param \App\picture $picture
|
81
|
+
* @return \Illuminate\Http\Response
|
82
|
+
*/
|
83
|
+
public function show(Request $request,$id,Picture $picture)
|
84
|
+
{
|
85
|
+
$message='This is your picture.'.$id;
|
86
|
+
$picture=Picture::find($id);
|
87
|
+
//$idに格納された番号と一致したデータを引っ張り出す。
|
88
|
+
return view('show',['message'=>$message,'picture'=>$picture]);
|
89
|
+
}
|
90
|
+
|
91
|
+
/**
|
92
|
+
* Show the form for editing the specified resource.
|
93
|
+
*
|
94
|
+
* @param \App\picture $picture
|
95
|
+
* @return \Illuminate\Http\Response
|
96
|
+
*/
|
97
|
+
public function edit(Request $request,$id,picture $picture)
|
98
|
+
{
|
99
|
+
$message='Edit your picture.'.$id;
|
100
|
+
$picture=Picture::find($id);
|
101
|
+
//$idに格納された番号と一致したデータを引っ張り出す。
|
102
|
+
return view('edit',['message'=>$message,'picture'=>$picture]);
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Update the specified resource in storage.
|
107
|
+
*
|
108
|
+
* @param \Illuminate\Http\Request $request
|
109
|
+
* @param \App\picture $picture
|
110
|
+
* @return \Illuminate\Http\Response
|
111
|
+
*/
|
112
|
+
public function update(Request $request,$id, picture $picture)
|
113
|
+
{
|
114
|
+
$picture=Picture::find($id);
|
115
|
+
//登録したブックマークをDBに格納させる
|
116
|
+
$picture->content=$request->content;
|
117
|
+
$picture->user_name=$request->user_name;
|
118
|
+
$picture->save();
|
119
|
+
//tinkerコマンドと同じ
|
120
|
+
return redirect()->route('picture.show',['id'=>$picture->id]);
|
121
|
+
}
|
122
|
+
|
123
|
+
/**
|
124
|
+
* Remove the specified resource from storage.
|
125
|
+
*
|
126
|
+
* @param \App\picture $picture
|
127
|
+
* @return \Illuminate\Http\Response
|
128
|
+
*/
|
129
|
+
public function destroy(Request $request,$id,picture $picture)
|
130
|
+
{
|
131
|
+
$picture=Picture::find($id);
|
132
|
+
$picture->delete();
|
133
|
+
return redirect('/pictures');
|
134
|
+
}
|
135
|
+
}
|
37
136
|
```
|
38
137
|
|
39
138
|
|
@@ -113,8 +212,116 @@
|
|
113
212
|
</div>
|
114
213
|
@endsection
|
115
214
|
```
|
215
|
+
```2020_01_05_062834_create_pictures_table.php
|
216
|
+
<?php
|
217
|
+
|
218
|
+
use Illuminate\Database\Migrations\Migration;
|
219
|
+
use Illuminate\Database\Schema\Blueprint;
|
220
|
+
use Illuminate\Support\Facades\Schema;
|
221
|
+
|
222
|
+
class CreatepicturesTable extends Migration
|
223
|
+
{
|
224
|
+
/**
|
225
|
+
* Run the migrations.
|
226
|
+
*
|
227
|
+
* @return void
|
228
|
+
*/
|
229
|
+
public function up()
|
230
|
+
{
|
231
|
+
Schema::create('pictures', function (Blueprint $table) {
|
232
|
+
$table->bigIncrements('id');
|
233
|
+
$table->string('content');
|
234
|
+
$table->timestamps();
|
235
|
+
});
|
236
|
+
}
|
237
|
+
|
238
|
+
/**
|
239
|
+
* Reverse the migrations.
|
240
|
+
*
|
241
|
+
* @return void
|
242
|
+
*/
|
243
|
+
public function down()
|
244
|
+
{
|
245
|
+
Schema::dropIfExists('pictures');
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
```
|
250
|
+
|
251
|
+
```2020_01_12_155013_add_column_username.php
|
252
|
+
<?php
|
253
|
+
|
254
|
+
use Illuminate\Database\Migrations\Migration;
|
255
|
+
use Illuminate\Database\Schema\Blueprint;
|
256
|
+
use Illuminate\Support\Facades\Schema;
|
257
|
+
|
258
|
+
class AddColumnUsername extends Migration
|
259
|
+
{
|
260
|
+
/**
|
261
|
+
* Run the migrations.
|
262
|
+
*
|
263
|
+
* @return void
|
264
|
+
*/
|
265
|
+
public function up()
|
266
|
+
{
|
267
|
+
Schema::table('pictures', function (Blueprint $table) {
|
268
|
+
$table->string('user_name');
|
269
|
+
});
|
270
|
+
}
|
271
|
+
|
272
|
+
/**
|
273
|
+
* Reverse the migrations.
|
274
|
+
*
|
275
|
+
* @return void
|
276
|
+
*/
|
277
|
+
public function down()
|
278
|
+
{
|
279
|
+
Schema::table('picture', function (Blueprint $table) {
|
280
|
+
//
|
281
|
+
});
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
285
|
+
```
|
286
|
+
```2020_05_27_145554_add_column_image.php
|
287
|
+
<?php
|
288
|
+
|
289
|
+
use Illuminate\Database\Migrations\Migration;
|
290
|
+
use Illuminate\Database\Schema\Blueprint;
|
291
|
+
use Illuminate\Support\Facades\Schema;
|
292
|
+
|
293
|
+
class AddColumnImage extends Migration
|
294
|
+
{
|
295
|
+
/**
|
296
|
+
* Run the migrations.
|
297
|
+
*
|
298
|
+
* @return void
|
299
|
+
*/
|
300
|
+
public function up()
|
301
|
+
{
|
302
|
+
Schema::table('pictures', function (Blueprint $table) {
|
303
|
+
//投稿した画像のファイルパスを格納
|
304
|
+
$table->string('image');
|
305
|
+
});
|
306
|
+
}
|
307
|
+
|
308
|
+
/**
|
309
|
+
* Reverse the migrations.
|
310
|
+
*
|
311
|
+
* @return void
|
312
|
+
*/
|
313
|
+
public function down()
|
314
|
+
{
|
315
|
+
Schema::table('pictures', function (Blueprint $table) {
|
316
|
+
//
|
317
|
+
});
|
318
|
+
}
|
319
|
+
}
|
320
|
+
|
321
|
+
```
|
322
|
+
|
116
323
|
以下、作成したDBのキャプチャです。
|
117
324
|
「image」カラム(String型)にファイルパスを格納するつもりです。
|
118
|
-
](e9281ac8a2e1f3bbb954bb301779ad60.png)
|
325
|
+
](e9281ac8a2e1f3bbb954bb301779ad60.png)
|
119
326
|
|
120
327
|
以上、不勉強で至らぬ所ではございますが、ご教授・ご助言をお願い致します。m(_ _)m
|