I think you should remove some line of code that no need, for example, the count because you are using 'foreach' so you don't need it but if you want to use count then use 'for' instead. And maybe better use true timestamps in the model rather than use Carbon every execution to the database.
For file handling, I suggest using
Laravel File Storage
Maybe like this
...
use App\Image;
use Storage;
...
// check for the request
if (!empty($request->image)) {
// check how many requests come
if (count($request->image) == 1) {
// do single upload
$uploadedFile = $request->file('file'); // be careful naming your request, I suggest use image or others..
$filelink = $uploadedFile->storeAs('public/images', time().'.'.$request->cover->getClientOriginalExtension()); // change the time() if you want to use the original name but I didn't recommend it because avoiding duplicated file
// do with your own style to store
$image = new Image;
$image->description = $request->description;
$image->category = $request->category;
$image->privacy = $request->privacy;
$image->filename = $filelink; // to get the file path
$image->property_id = $request->id; // idk what is this
if ($image->save()) {
return redirect('settings/photos');
}
// or something else
}
else {
// for multiple request use foreach instead
foreach ($request->image as $item) {
// do upload and store
$uploadedFile = $request->file('file'); // be careful naming your request, I suggest use image or others..
$filelink = $uploadedFile->storeAs('public/images', time().'.'.$request->cover->getClientOriginalExtension()); // change the time() if you want to use the original name but I didn't recommend it because avoiding duplicated file
// do with your own style to store
$image = new Image;
$image->description = $request->description;
$image->category = $request->category;
$image->privacy = $request->privacy;
$image->filename = $filelink; // to get the file path
$image->property_id = $request->id; // idk what is this
if ($image->save()) {
return redirect('settings/photos');
}
// or something else
}
}
// or something else
}
It's my style btw, I haven't tested it out hope it's work haha
Did you tell your Form you'll be uploading files and multiple files?
{{Form::open(['route'=>'uploadImage', 'files' => true])}} {{ Form::file('photo[]', ['multiple', 'class'=>'form-control']) }}