@bjbt99 have you got enctype='multipart/form-data' set on your form?
Image Upload in Laravel 7
Hey guys I have been stuck on this for a few days now so hopefully someone can give me some sort of insight.
So I am trying to be able to upload an image for a specific store. When I upload the image the page simply refreshes and the file won't be in DB or project structure.
Here is my controller:
<?php
namespace App\Http\Controllers;
use App\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class SuperadminStoresController extends Controller
{
/**
* SuperadminStoresController constructor.
*/
public function __construct()
{
// $this->middleware('IsSuperAdmin');
}
public function index()
{
$stores = Store::get();
return view('superadmin.stores.index', compact('stores'));
}
public function update(Request $request)
{
$values = request('stores');
$store_ids = array_keys($values);
$stores = Store::whereIn('id', $store_ids)->get();
foreach ($stores as $store)
{
if ($request->hasFile('logo_path')) {
$logo = $request->file($values[$store->id]['logo_path']);
$extension = $logo->getClientOriginalExtension();
$storedName = $logo . '.' . $extension;
Storage::disk('public')->put($storedName, File::get($logo));
} else {
$storedName = null;
}
$store->update([
'twilio_number' => $values[$store->id]['twilio_number'],
'logo_path' => $values[$store->id]['logo_path'],
]);
//
// if ($request->hasFile($values[$store->id]['logo_path'])) {
// $fileNameWithExt = $request->file($values[$store->id]['logo_path'])->getClientOriginalName();
// $fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// $extension = $request->file($values[$store->id]['logo_path'])->getClientOriginalExtension();
// $storedName = $fileName . '.' . $extension;
// Storage::putFileAs('/storage/uploaded_files/logos',
// $request->file($values[$store->id]['logo_path']), $storedName);
// } else {
// $storedName = null;
// }
}
return redirect()->back();
}
}
The commented out section in the foreach above was something I tried but didnt have any luck in. Left it there just in case.
Here is my filesystems.php public driver where I am trying to store the image:
'public' => [
'driver' => 'local',
'root' => storage_path() . '/app/storage/uploaded_files/logos',
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
And I have a stores DB that I have a field logo_path where I am attempting to save the file path. Any help would be greatly appreciated let me know if I can give any more information!
Please or to participate in this conversation.