feat: add image upload trait

This commit is contained in:
2026-06-03 03:05:11 +03:30
parent 6f01c783fd
commit 79303de0fb
3 changed files with 62 additions and 8 deletions
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace App\Traits;
use App\Models\Image;
use Illuminate\Http\Request;
trait HandlesImageUpload
{
/**
* If the request carries an uploaded image file, store it, create an Image
* record for it, and return that record's id. Returns null when no file is
* present so callers can fall back to a provided image_id.
*/
protected function uploadedImageId(Request $request, string $field = 'image'): ?int
{
if (!$request->hasFile($field)) {
return null;
}
$path = $request->file($field)->store('images', 'public');
return Image::create([
'user_id' => auth()->id(),
'path' => $path,
'type' => 'public',
])->id;
}
}