質問編集履歴

3

bladeの変更

2020/08/30 04:17

投稿

m2B
m2B

スコア20

test CHANGED
File without changes
test CHANGED
@@ -208,17 +208,13 @@
208
208
 
209
209
  ```Blade
210
210
 
211
- <label for="price">Category</label>
212
-
213
- <div class="c-post c-post--inputWidth">
214
-
215
- <select name="category" class="c-form--control" id="">
211
+ <select name="category_id" class="c-form--control" id="">
216
212
 
217
213
  <option value="">選択してください</option>
218
214
 
219
215
  @foreach ($categories as $item => $value)
220
216
 
221
- <option value="{{ $item }}" {{ $item === old('id', $value['id'] ) ? 'selected' : ''}}>{{ $value['name'] }}</option>
217
+ <option value="{{ $item }}" @if(old('category_id') == $item) selected @endif >{{ $value }}</option>
222
218
 
223
219
  {{-- <option value="{{$item}}" @if(old('id') == $categories['id']) selected @endif>{{$value['name']}}</option> --}}
224
220
 
@@ -226,8 +222,6 @@
226
222
 
227
223
  </select>
228
224
 
229
- </div>
230
-
231
225
  ```
232
226
 
233
227
  ```Model

2

Requestの追加

2020/08/30 04:16

投稿

m2B
m2B

スコア20

test CHANGED
File without changes
test CHANGED
@@ -120,7 +120,9 @@
120
120
 
121
121
  // $category = Category::where('id')->first();
122
122
 
123
- $categories = Category::all();
123
+ // $categories = Category::all();
124
+
125
+      $categories = Category::pluck('name', 'id')->toArray();
124
126
 
125
127
  $allTagNames = Tag::all()->map(function($tag) {
126
128
 
@@ -154,7 +156,7 @@
154
156
 
155
157
  $product->user_id = $request->user()->id;
156
158
 
157
-
159
+ $product->category_id = $request->category()->id;
158
160
 
159
161
  // $filename = $request->file('photo')->store('public');
160
162
 
@@ -178,7 +180,7 @@
178
180
 
179
181
 
180
182
 
181
- $request->category = $product->category() ;
183
+ // $request->category = $product->category() ;
182
184
 
183
185
 
184
186
 
@@ -274,6 +276,114 @@
274
276
 
275
277
  ```
276
278
 
279
+
280
+
281
+ ```ProductRequest
282
+
283
+ <?php
284
+
285
+
286
+
287
+ namespace App\Http\Requests;
288
+
289
+
290
+
291
+ use Illuminate\Foundation\Http\FormRequest;
292
+
293
+
294
+
295
+ class ProductRequest extends FormRequest
296
+
297
+ {
298
+
299
+ /**
300
+
301
+ * Determine if the user is authorized to make this request.
302
+
303
+ *
304
+
305
+ * @return bool
306
+
307
+ */
308
+
309
+ public function authorize()
310
+
311
+ {
312
+
313
+ return true;
314
+
315
+ }
316
+
317
+
318
+
319
+ /**
320
+
321
+ * Get the validation rules that apply to the request.
322
+
323
+ *
324
+
325
+ * @return array
326
+
327
+ */
328
+
329
+ public function rules()
330
+
331
+ {
332
+
333
+ return [
334
+
335
+ 'title' => 'required|max:30',
336
+
337
+ 'review' => 'required|max:200',
338
+
339
+ // 'category_id' => 'required',
340
+
341
+ 'tags' => 'json|regex:/^(?!.*\s).+$/u|regex:/^(?!.*/).*$/u',
342
+
343
+ ];
344
+
345
+ }
346
+
347
+
348
+
349
+ public function attributes()
350
+
351
+ {
352
+
353
+ return [
354
+
355
+ 'title' => 'タイトル',
356
+
357
+ 'review' => 'レビュー',
358
+
359
+ // 'category_id' => 'カテゴリー',
360
+
361
+ 'tags' => 'タグ',
362
+
363
+ ];
364
+
365
+ }
366
+
367
+
368
+
369
+ public function passedValidation()
370
+
371
+ {
372
+
373
+ $this->tags = collect(json_decode($this->tags))->slice(0, 5)->map(function($requestTag) {
374
+
375
+ return $requestTag->text;
376
+
377
+ });
378
+
379
+ }
380
+
381
+ }
382
+
383
+
384
+
385
+ ```
386
+
277
387
  ### 試したこと
278
388
 
279
389
 

1

誤字と修正追加

2020/08/30 04:15

投稿

m2B
m2B

スコア20

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,95 @@
14
14
 
15
15
  post送信して,
16
16
 
17
- データは入っているのですが、一番最初のidのデーしか入らないです。
17
+ データは入っているのですが、一番最初のidのデーしか入らないです。
18
+
18
-
19
+ dd($categories)の内容が
20
+
19
- ```
21
+ ```
22
+
20
-
23
+ Illuminate\Database\Eloquent\Collection {#1253 ▼
24
+
25
+ #items: array:6 [▼
26
+
27
+ 0 => App\Category {#1254 ▼
28
+
29
+ #fillable: array:1 [▶]
30
+
31
+ #connection: "mysql"
32
+
33
+ #table: "categories"
34
+
35
+ #primaryKey: "id"
36
+
37
+ #keyType: "int"
38
+
39
+ +incrementing: true
40
+
21
- エラーメッセージ
41
+ #with: []
42
+
43
+ #withCount: []
44
+
45
+ #perPage: 15
46
+
47
+ +exists: true
48
+
49
+ +wasRecentlyCreated: false
50
+
51
+ #attributes: array:4 [▼
52
+
53
+ "id" => 1
54
+
55
+ "name" => "家電/スマホ/カメラ"
56
+
57
+ "created_at" => null
58
+
59
+ "updated_at" => null
60
+
61
+ ]
62
+
63
+ #original: array:4 [▶]
64
+
65
+ #changes: []
66
+
67
+ #casts: []
68
+
69
+ #dates: []
70
+
71
+ #dateFormat: null
72
+
73
+ #appends: []
74
+
75
+ #dispatchesEvents: []
76
+
77
+ #observables: []
78
+
79
+ #relations: []
80
+
81
+ #touches: []
82
+
83
+ +timestamps: true
84
+
85
+ #hidden: []
86
+
87
+ #visible: []
88
+
89
+ #guarded: array:1 [▶]
90
+
91
+ }
92
+
93
+ 1 => App\Category {#1255 ▶}
94
+
95
+ 2 => App\Category {#1256 ▶}
96
+
97
+ 3 => App\Category {#1257 ▶}
98
+
99
+ 4 => App\Category {#1258 ▶}
100
+
101
+ 5 => App\Category {#1259 ▶}
102
+
103
+ ]
104
+
105
+ }
22
106
 
23
107
  ```
24
108