teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

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

2019/08/30 02:40

投稿

Yachin
Yachin

スコア21

answer CHANGED
@@ -1,3 +1,207 @@
1
- はじめはshopsstableにイメージファイルのパス、picstableにイメージの詳細を貼り付け、関連づけようとしていました。
1
+ はじめはshopstableにイメージファイルのパス、picstableにイメージの詳細を貼り付け、関連づけようとしていました。
2
2
  しかしながら、自身の漠然とした方法と知見の不足から、ご丁寧な回答をいただいても解決できるところまでには及びませんでした。
3
- 最終的にはpicstableを使用するのをやめて、'php artisan storage:link'とコマンド入力するだけで解決することができました。
3
+ 最終的にはpicstableを使用するのをやめて、'php artisan storage:link'とコマンド入力し、コードを整理すると解決することができました。
4
+ 以下、コードを記載します。
5
+
6
+ ---
7
+ /Applications/MAMP/htdocs/ramenmap/resources/views/index.blade.php
8
+ ```
9
+ @extends('layout')
10
+ @section('content')
11
+ @auth
12
+ <div class="text-right">
13
+ <a href={{ route('shop.new') }} class='btn btn-outline-info'>お店投稿</a>
14
+ </div>
15
+ @endauth
16
+ <h1>お店一覧</h1>
17
+ @foreach ($shops as $shop)
18
+ <div class="card mb-3" style="max-width: 540px;">
19
+ <div class="row no-gutters">
20
+ <div class="col-md-4">
21
+ <img src="{{ asset('storage/image/'.$shop->image) }}" class="card-img">
22
+
23
+ </div>
24
+ <div class="col-md-8">
25
+ <div class="card-body">
26
+ <h5 class="card-title"> <a href={{ route('shop.detail', ['id' => $shop->id]) }}>{{ $shop->name }}</a>
27
+ </h5>
28
+ <p class="card-text">{{ $shop->category->name }}
29
+ @if ($shop->category->name != $shop->subcategory->name)
30
+ / {{ $shop->subcategory->name }}
31
+ @endif
32
+ <p class="card-text">{{ $shop->address }}</p>
33
+ <p class="card-text">{{ $shop->user->name }}</p>
34
+ <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
35
+ </div>
36
+ </div>
37
+ </div>
38
+ </div>
39
+ @endforeach
40
+ @endsection
41
+ ```
42
+ ---
43
+ /Applications/MAMP/htdocs/ramenmap/app/Shop.php
44
+ ```
45
+ <?php
46
+
47
+ namespace App;
48
+
49
+ use Illuminate\Database\Eloquent\Model;
50
+
51
+
52
+ class Shop extends Model
53
+ {
54
+ protected $fillable = [
55
+ 'name', 'address', 'category_id', 'subcategory_id', 'user_id', 'image',
56
+ ];
57
+
58
+ public function category()
59
+ {
60
+ return $this->belongsTo('App\Category');
61
+ }
62
+
63
+ public function subcategory()
64
+ {
65
+ return $this->belongsTo('App\SubCategory');
66
+ }
67
+
68
+ public function user()
69
+ {
70
+ return $this->belongsTo('App\User');
71
+ }
72
+ }
73
+ ```
74
+ ---
75
+ /Applications/MAMP/htdocs/ramenmap/app/Http/Controllers/ShopController.php
76
+ ```
77
+ <?php
78
+
79
+ namespace App\Http\Controllers;
80
+
81
+ use App\Shop;
82
+ use App\Category;
83
+ use App\SubCategory;
84
+ use Illuminate\Http\Request;
85
+
86
+ class ShopController extends Controller
87
+ {
88
+ public function __construct()
89
+ {
90
+ $this->middleware('auth')->except(['index', 'show']);
91
+ }
92
+ /**
93
+ * Display a listing of the resource.
94
+ *
95
+ * @return \Illuminate\Http\Response
96
+ */
97
+ public function index()
98
+ {
99
+ $shops = Shop::latest()->get();
100
+ $image = Shop::all();
101
+ return view('index', ['shops'=>$shops, 'image'=>$image]);
102
+ }
103
+
104
+ /**
105
+ * Show the form for creating a new resource.
106
+ *
107
+ * @return \Illuminate\Http\Response
108
+ */
109
+ public function create(Request $request)
110
+ {
111
+ $shop = new Shop();
112
+ $shop->name = $request->name;
113
+ $categories = Category::all()->pluck('name', 'id');
114
+ $subcategories = SubCategory::all()->pluck('name', 'id');
115
+ return view('new', ['categories' => $categories, 'subcategories' => $subcategories,
116
+ ]);
117
+ }
118
+
119
+ /**
120
+ * Store a newly created resource in storage.
121
+ *
122
+ * @param \Illuminate\Http\Request $request
123
+ * @return \Illuminate\Http\Response
124
+ */
125
+ public function store(Request $request)
126
+ {
127
+ $shop = new Shop;
128
+ $user = \Auth::user();
129
+
130
+ $shop->name = request('name');
131
+ $shop->address= request('address');
132
+ $shop->category_id = request('category_id');
133
+ $shop->subcategory_id = request('subcategory_id');
134
+ $shop->user_id = $user->id;
135
+ $filename = $request->file('image')->store('public/image');
136
+ $shop->image = basename($filename);
137
+ $shop->save();
138
+ return redirect()->route('shop.detail', ['id' => $shop->id]);
139
+ }
140
+
141
+ /**
142
+ * Display the specified resource.
143
+ *
144
+ * @param \App\Shop $shop
145
+ * @return \Illuminate\Http\Response
146
+ */
147
+ public function show($id)
148
+ {
149
+ $shop = Shop::find($id);
150
+ $user = \Auth::user();
151
+ if ($user) {
152
+ $login_user_id = $user->id;
153
+ } else {
154
+ $login_user_id = '';
155
+ }
156
+
157
+ return view('show', ['shop' => $shop, 'login_user_id'=>$login_user_id]);
158
+ }
159
+
160
+ /**
161
+ * Show the form for editing the specified resource.
162
+ *
163
+ * @param \App\Shop $shop
164
+ * @return \Illuminate\Http\Response
165
+ */
166
+ public function edit(Shop $shop, $id)
167
+ {
168
+ $shop = Shop::find($id);
169
+ $categories = Category::all()->pluck('name', 'id');
170
+ $subcategories = SubCategory::all()->pluck('name', 'id');
171
+ return view('edit', ['shop' => $shop, 'categories' => $categories, 'subcategories' => $subcategories]);
172
+ }
173
+
174
+ /**
175
+ * Update the specified resource in storage.
176
+ *
177
+ * @param \Illuminate\Http\Request $request
178
+ * @param \App\Shop $shop
179
+ * @return \Illuminate\Http\Response
180
+ */
181
+ public function update(Request $request, Shop $shop, $id)
182
+ {
183
+ $shop = Shop::find($id);
184
+ $shop->name = request('name');
185
+ $shop->address = request('address');
186
+ $shop->category_id = request('category_id');
187
+ $shop->subcategory_id = request('subcategory_id');
188
+ $filename = $request->file('image')->store('public/image');
189
+ $shop->image = basename($filename);
190
+ $shop->save();
191
+ return redirect()->route('shop.detail', ['id' => $shop->id]);
192
+ }
193
+
194
+ /**
195
+ * Remove the specified resource from storage.
196
+ *
197
+ * @param \App\Shop $shop
198
+ * @return \Illuminate\Http\Response
199
+ */
200
+ public function destroy($id)
201
+ {
202
+ $shop = Shop::find($id);
203
+ $shop->destroy($id);
204
+ return redirect('/shops');
205
+ }
206
+ }
207
+ ```