Skip to content

Commit d692cec

Browse files
author
ahmadhuss
committed
refactor: ProductController.php will automatically detect storage
* Is it local environment or production environment?
1 parent 4bb85be commit d692cec

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

app/Http/Controllers/ProductController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Http\Helpers\Environment;
56
use App\Http\Requests\StoreProductRequest;
67
use App\Models\Category;
78
use App\Models\Product;
89
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Storage;
911

1012
class ProductController extends Controller
1113
{
14+
use Environment;
1215
/**
1316
* Display a listing of the resource.
1417
*
@@ -64,15 +67,14 @@ public function store(StoreProductRequest $request)
6467
* "php artisan storage:link"
6568
* It will create the symlink.
6669
*/
67-
$path = $request->file('photo')->store('photos', 'public');
68-
//dd($path); //dump&die
70+
$path = $request->file('photo')->store('photos', self::getStorageEnvironment());
6971

7072
Product::create([
7173
'name' => $request->name,
7274
'price' => $request->price,
7375
'category_id' => $request->category_id,
7476
'description' => $request->description,
75-
'photo' => $path
77+
'photo' => Storage::disk(self::getStorageEnvironment())->url($path)
7678
]);
7779
return redirect()->route('products.index');
7880
}
@@ -110,7 +112,7 @@ public function edit($id)
110112
*/
111113
public function update(StoreProductRequest $request, $id)
112114
{
113-
$path = $request->file('photo')->store('photos', 'public');
115+
$path = $request->file('photo')->store('photos', self::getStorageEnvironment());
114116
$product = Product::findOrFail($id);
115117
$product->update([
116118
'name' => $request->name,

0 commit comments

Comments
 (0)