質問編集履歴

3

コード変更 モデルコード追加

2018/12/28 07:17

投稿

Ryosukevvvv
Ryosukevvvv

スコア19

test CHANGED
File without changes
test CHANGED
@@ -1,14 +1,138 @@
1
+ 以下のように
2
+
1
- 以下ようにレシピコントローラと詳細ページを書いているのですが
3
+ Recipes.controllerとrecipes.showのレシピ詳細ページを書いているのですが
4
+
5
+
6
+
2
-
7
+ ```
8
+
9
+ // getでrecipes/idにアクセスされた場合の「取得表示処理」
10
+
11
+ public function show($id)
12
+
13
+ {
14
+
15
+ $recipe = Recipe::find($id);
16
+
3
- ![イメージ説明](93cdc47ea63b0621278ee4c279f4cd2a.png)
17
+ $ingredients = Recipe::find($id)->ingredients;
18
+
4
-
19
+ $how_to = Recipe::find($id)->how_to;
20
+
21
+
22
+
23
+ return view('recipes.show',[
24
+
25
+ 'recipe' => $recipe,
26
+
5
- ![イメージ説明](e8567aad827d45c5b8f9e7f01db05e92.png)
27
+ 'ingredients' => $ingredients,
28
+
6
-
29
+ 'how_to' => $how_to,
30
+
31
+ ]);
32
+
33
+ }
34
+
35
+ ```
36
+
37
+
38
+
39
+ ```
40
+
41
+ //recipes.showのレシピ詳細ページ
42
+
43
+ <div class="container">
44
+
45
+ <div class="row">
46
+
47
+ <div class="col-4">
48
+
49
+ <h2>{{ $recipe->name }}</h2>
50
+
51
+ </div>
52
+
53
+ <div class="col-7 offset-1">
54
+
55
+ <h3>{{ $recipe->content }}</h3>
56
+
57
+ </div>
58
+
59
+ </div>
60
+
61
+ </div>
62
+
63
+ <div class="container">
64
+
65
+ <div class="row">
66
+
67
+ <div class="col-4">
68
+
69
+ <img src="https://placehold.jp/200x200.png"></img>
70
+
71
+ </div>
72
+
73
+ <div class="col-8">
74
+
75
+ <h4>材料(2人分)</h4>
76
+
77
+ <div class="row">
78
+
79
+ <div class="col-md-8">
80
+
81
+ <h4>材料・調味料名</h4>
82
+
7
- ![イメージ説明](8d884c5899d201ac664205f393088dde.png)
83
+ @foreach ($ingredients as $ingredient)
84
+
8
-
85
+ <div>
86
+
87
+ {{ $ingredient->ingredient }}
88
+
89
+ </div>
90
+
91
+ @endforeach
92
+
93
+ </div>
94
+
9
- ![イメージ説明](9b0e41001b7af20ccc7948b21438b87a.png)
95
+ <div class="col-md-4">
96
+
10
-
97
+ <h4>分量</h4>
98
+
11
- ![イメージ説明](1b57bce529ea08a010c539ba612d6b55.png)
99
+ @foreach ($ingredients as $ingredient)
100
+
101
+ <div>
102
+
103
+ {{ $ingredient->quantity }}
104
+
105
+ </div>
106
+
107
+ @endforeach
108
+
109
+ </div>
110
+
111
+ </div>
112
+
113
+ </div>
114
+
115
+ </div>
116
+
117
+ </div>
118
+
119
+ <div class="container">
120
+
121
+ <h4>作り方</h4>
122
+
123
+ @foreach ($how_tos as $how_to)
124
+
125
+ <div>
126
+
127
+ {{ $how_to->how_to_make }}
128
+
129
+ </div>
130
+
131
+ @endforeach
132
+
133
+ </div>
134
+
135
+ ```
12
136
 
13
137
  以下のようなエラーが起きて材料、分量、作り方が取得できません。
14
138
 
@@ -16,12 +140,118 @@
16
140
 
17
141
  どのように変更すればいいのでしょうか?
18
142
 
19
- このようにテーブル設計をしています。
143
+
20
-
21
- ![イメージ説明](3301309338996e16c0fb5cb410eb26ea.png)
22
144
 
23
145
  1対多のリレーションは済んでいます。
24
146
 
25
-
147
+ ```
148
+
149
+ namespace App;
150
+
151
+
152
+
153
+ use Illuminate\Database\Eloquent\Model;
154
+
155
+
156
+
157
+ class Recipe extends Model
158
+
159
+ {
160
+
161
+ protected $fillable = ['user_id', 'name', 'content', 'photo_url'];
162
+
163
+
164
+
165
+ public function user()
166
+
167
+ {
168
+
169
+ return $this->belongsTo(User::class);
170
+
171
+ }
172
+
173
+
174
+
175
+ public function ingredient()
176
+
177
+ {
178
+
179
+ return $this->hasMany(Ingredient::class);
180
+
181
+ }
182
+
183
+
184
+
185
+ public function how_to()
186
+
187
+ {
188
+
189
+ return $this->hasMany(HowTo::class);
190
+
191
+ }
192
+
193
+ }
194
+
195
+ ```
196
+
197
+ ```
198
+
199
+ namespace App;
200
+
201
+
202
+
203
+ use Illuminate\Database\Eloquent\Model;
204
+
205
+
206
+
207
+ class Ingredient extends Model
208
+
209
+ {
210
+
211
+ protected $fillable = ['recipe_id', 'ingredient', 'quantity'];
212
+
213
+
214
+
215
+ public function recipe()
216
+
217
+ {
218
+
219
+ return $this->belongsTo(Recipe::class);
220
+
221
+ }
222
+
223
+ }
224
+
225
+ ```
226
+
227
+ ```
228
+
229
+ namespace App;
230
+
231
+
232
+
233
+ use Illuminate\Database\Eloquent\Model;
234
+
235
+
236
+
237
+ class HowTo extends Model
238
+
239
+ {
240
+
241
+ protected $fillable = ['recipe_id', 'how_to_make'];
242
+
243
+
244
+
245
+ public function recipe()
246
+
247
+ {
248
+
249
+ return $this->belongsTo(Recipe::class);
250
+
251
+ }
252
+
253
+ }
254
+
255
+ ```
26
256
 
27
257
  回答よろしくお願いします。

2

エラー文

2018/12/28 07:17

投稿

Ryosukevvvv
Ryosukevvvv

スコア19

test CHANGED
File without changes
test CHANGED
@@ -10,19 +10,17 @@
10
10
 
11
11
  ![イメージ説明](1b57bce529ea08a010c539ba612d6b55.png)
12
12
 
13
- 以下のようなエラーが起きて名前、コメント、材料、分量、作り方が取得できません。
13
+ 以下のようなエラーが起きて材料、分量、作り方が取得できません。
14
14
 
15
- ![イメージ説明](1b25a0ad46b25009ea72e615ae08aa23.png)
15
+ ![イメージ説明](e4a94f236c780f5d4cce5fbc8367225e.png)
16
16
 
17
-
18
-
19
- こを変更すればいいのでしょうか?
17
+ のように変更すればいいのでしょうか?
20
18
 
21
19
  このようにテーブル設計をしています。
22
20
 
21
+ ![イメージ説明](3301309338996e16c0fb5cb410eb26ea.png)
22
+
23
23
  1対多のリレーションは済んでいます。
24
-
25
- ![イメージ説明](2218d1593aec10131e1264a8ad3b9d94.png)
26
24
 
27
25
 
28
26
 

1

Laravel5.5

2018/12/27 20:25

投稿

Ryosukevvvv
Ryosukevvvv

スコア19

test CHANGED
@@ -1 +1 @@
1
- Laravel リレーション カラム取得ができない
1
+ Laravel5.5 リレーション カラム取得ができない
test CHANGED
File without changes