回答編集履歴

1

自己解決でなんとかなりました。

2019/08/30 02:40

投稿

Yachin
Yachin

スコア21

test CHANGED
@@ -1,5 +1,413 @@
1
- はじめはshopsstableにイメージファイルのパス、picstableにイメージの詳細を貼り付け、関連づけようとしていました。
1
+ はじめはshopstableにイメージファイルのパス、picstableにイメージの詳細を貼り付け、関連づけようとしていました。
2
2
 
3
3
  しかしながら、自身の漠然とした方法と知見の不足から、ご丁寧な回答をいただいても解決できるところまでには及びませんでした。
4
4
 
5
- 最終的にはpicstableを使用するのをやめて、'php artisan storage:link'とコマンド入力するだけで解決することができました。
5
+ 最終的にはpicstableを使用するのをやめて、'php artisan storage:link'とコマンド入力し、コードを整理すると解決することができました。
6
+
7
+ 以下、コードを記載します。
8
+
9
+
10
+
11
+ ---
12
+
13
+ /Applications/MAMP/htdocs/ramenmap/resources/views/index.blade.php
14
+
15
+ ```
16
+
17
+ @extends('layout')
18
+
19
+ @section('content')
20
+
21
+ @auth
22
+
23
+ <div class="text-right">
24
+
25
+ <a href={{ route('shop.new') }} class='btn btn-outline-info'>お店投稿</a>
26
+
27
+ </div>
28
+
29
+ @endauth
30
+
31
+ <h1>お店一覧</h1>
32
+
33
+ @foreach ($shops as $shop)
34
+
35
+ <div class="card mb-3" style="max-width: 540px;">
36
+
37
+ <div class="row no-gutters">
38
+
39
+ <div class="col-md-4">
40
+
41
+ <img src="{{ asset('storage/image/'.$shop->image) }}" class="card-img">
42
+
43
+
44
+
45
+ </div>
46
+
47
+ <div class="col-md-8">
48
+
49
+ <div class="card-body">
50
+
51
+ <h5 class="card-title"> <a href={{ route('shop.detail', ['id' => $shop->id]) }}>{{ $shop->name }}</a>
52
+
53
+ </h5>
54
+
55
+ <p class="card-text">{{ $shop->category->name }}
56
+
57
+ @if ($shop->category->name != $shop->subcategory->name)
58
+
59
+ / {{ $shop->subcategory->name }}
60
+
61
+ @endif
62
+
63
+ <p class="card-text">{{ $shop->address }}</p>
64
+
65
+ <p class="card-text">{{ $shop->user->name }}</p>
66
+
67
+ <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
68
+
69
+ </div>
70
+
71
+ </div>
72
+
73
+ </div>
74
+
75
+ </div>
76
+
77
+ @endforeach
78
+
79
+ @endsection
80
+
81
+ ```
82
+
83
+ ---
84
+
85
+ /Applications/MAMP/htdocs/ramenmap/app/Shop.php
86
+
87
+ ```
88
+
89
+ <?php
90
+
91
+
92
+
93
+ namespace App;
94
+
95
+
96
+
97
+ use Illuminate\Database\Eloquent\Model;
98
+
99
+
100
+
101
+
102
+
103
+ class Shop extends Model
104
+
105
+ {
106
+
107
+ protected $fillable = [
108
+
109
+ 'name', 'address', 'category_id', 'subcategory_id', 'user_id', 'image',
110
+
111
+ ];
112
+
113
+
114
+
115
+ public function category()
116
+
117
+ {
118
+
119
+ return $this->belongsTo('App\Category');
120
+
121
+ }
122
+
123
+
124
+
125
+ public function subcategory()
126
+
127
+ {
128
+
129
+ return $this->belongsTo('App\SubCategory');
130
+
131
+ }
132
+
133
+
134
+
135
+ public function user()
136
+
137
+ {
138
+
139
+ return $this->belongsTo('App\User');
140
+
141
+ }
142
+
143
+ }
144
+
145
+ ```
146
+
147
+ ---
148
+
149
+ /Applications/MAMP/htdocs/ramenmap/app/Http/Controllers/ShopController.php
150
+
151
+ ```
152
+
153
+ <?php
154
+
155
+
156
+
157
+ namespace App\Http\Controllers;
158
+
159
+
160
+
161
+ use App\Shop;
162
+
163
+ use App\Category;
164
+
165
+ use App\SubCategory;
166
+
167
+ use Illuminate\Http\Request;
168
+
169
+
170
+
171
+ class ShopController extends Controller
172
+
173
+ {
174
+
175
+ public function __construct()
176
+
177
+ {
178
+
179
+ $this->middleware('auth')->except(['index', 'show']);
180
+
181
+ }
182
+
183
+ /**
184
+
185
+ * Display a listing of the resource.
186
+
187
+ *
188
+
189
+ * @return \Illuminate\Http\Response
190
+
191
+ */
192
+
193
+ public function index()
194
+
195
+ {
196
+
197
+ $shops = Shop::latest()->get();
198
+
199
+ $image = Shop::all();
200
+
201
+ return view('index', ['shops'=>$shops, 'image'=>$image]);
202
+
203
+ }
204
+
205
+
206
+
207
+ /**
208
+
209
+ * Show the form for creating a new resource.
210
+
211
+ *
212
+
213
+ * @return \Illuminate\Http\Response
214
+
215
+ */
216
+
217
+ public function create(Request $request)
218
+
219
+ {
220
+
221
+ $shop = new Shop();
222
+
223
+ $shop->name = $request->name;
224
+
225
+ $categories = Category::all()->pluck('name', 'id');
226
+
227
+ $subcategories = SubCategory::all()->pluck('name', 'id');
228
+
229
+ return view('new', ['categories' => $categories, 'subcategories' => $subcategories,
230
+
231
+ ]);
232
+
233
+ }
234
+
235
+
236
+
237
+ /**
238
+
239
+ * Store a newly created resource in storage.
240
+
241
+ *
242
+
243
+ * @param \Illuminate\Http\Request $request
244
+
245
+ * @return \Illuminate\Http\Response
246
+
247
+ */
248
+
249
+ public function store(Request $request)
250
+
251
+ {
252
+
253
+ $shop = new Shop;
254
+
255
+ $user = \Auth::user();
256
+
257
+
258
+
259
+ $shop->name = request('name');
260
+
261
+ $shop->address= request('address');
262
+
263
+ $shop->category_id = request('category_id');
264
+
265
+ $shop->subcategory_id = request('subcategory_id');
266
+
267
+ $shop->user_id = $user->id;
268
+
269
+ $filename = $request->file('image')->store('public/image');
270
+
271
+ $shop->image = basename($filename);
272
+
273
+ $shop->save();
274
+
275
+ return redirect()->route('shop.detail', ['id' => $shop->id]);
276
+
277
+ }
278
+
279
+
280
+
281
+ /**
282
+
283
+ * Display the specified resource.
284
+
285
+ *
286
+
287
+ * @param \App\Shop $shop
288
+
289
+ * @return \Illuminate\Http\Response
290
+
291
+ */
292
+
293
+ public function show($id)
294
+
295
+ {
296
+
297
+ $shop = Shop::find($id);
298
+
299
+ $user = \Auth::user();
300
+
301
+ if ($user) {
302
+
303
+ $login_user_id = $user->id;
304
+
305
+ } else {
306
+
307
+ $login_user_id = '';
308
+
309
+ }
310
+
311
+
312
+
313
+ return view('show', ['shop' => $shop, 'login_user_id'=>$login_user_id]);
314
+
315
+ }
316
+
317
+
318
+
319
+ /**
320
+
321
+ * Show the form for editing the specified resource.
322
+
323
+ *
324
+
325
+ * @param \App\Shop $shop
326
+
327
+ * @return \Illuminate\Http\Response
328
+
329
+ */
330
+
331
+ public function edit(Shop $shop, $id)
332
+
333
+ {
334
+
335
+ $shop = Shop::find($id);
336
+
337
+ $categories = Category::all()->pluck('name', 'id');
338
+
339
+ $subcategories = SubCategory::all()->pluck('name', 'id');
340
+
341
+ return view('edit', ['shop' => $shop, 'categories' => $categories, 'subcategories' => $subcategories]);
342
+
343
+ }
344
+
345
+
346
+
347
+ /**
348
+
349
+ * Update the specified resource in storage.
350
+
351
+ *
352
+
353
+ * @param \Illuminate\Http\Request $request
354
+
355
+ * @param \App\Shop $shop
356
+
357
+ * @return \Illuminate\Http\Response
358
+
359
+ */
360
+
361
+ public function update(Request $request, Shop $shop, $id)
362
+
363
+ {
364
+
365
+ $shop = Shop::find($id);
366
+
367
+ $shop->name = request('name');
368
+
369
+ $shop->address = request('address');
370
+
371
+ $shop->category_id = request('category_id');
372
+
373
+ $shop->subcategory_id = request('subcategory_id');
374
+
375
+ $filename = $request->file('image')->store('public/image');
376
+
377
+ $shop->image = basename($filename);
378
+
379
+ $shop->save();
380
+
381
+ return redirect()->route('shop.detail', ['id' => $shop->id]);
382
+
383
+ }
384
+
385
+
386
+
387
+ /**
388
+
389
+ * Remove the specified resource from storage.
390
+
391
+ *
392
+
393
+ * @param \App\Shop $shop
394
+
395
+ * @return \Illuminate\Http\Response
396
+
397
+ */
398
+
399
+ public function destroy($id)
400
+
401
+ {
402
+
403
+ $shop = Shop::find($id);
404
+
405
+ $shop->destroy($id);
406
+
407
+ return redirect('/shops');
408
+
409
+ }
410
+
411
+ }
412
+
413
+ ```