category qosishsh to'g'irlandi

This commit is contained in:
2026-04-29 13:53:09 +05:00
parent 6937deecbe
commit ac2c25bd06
3 changed files with 48 additions and 4 deletions

View File

@@ -2,9 +2,10 @@
namespace App\Http\Requests\Dashboard\Category; namespace App\Http\Requests\Dashboard\Category;
use App\Support\Uploads;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use RuntimeException;
class Request extends FormRequest class Request extends FormRequest
{ {
@@ -48,12 +49,31 @@ class Request extends FormRequest
public function getImage(): string public function getImage(): string
{ {
if ($this->hasFile('image')) { if ($this->hasFile('image')) {
return Uploads::store($this->file('image'), 'uploads/categories', 'local'); return $this->storeCategoryImage($this->file('image'));
} else { } else {
return 'null'; return 'null';
} }
} }
private function storeCategoryImage(UploadedFile $file): string
{
$relativeDirectory = 'uploads/categories';
$directory = public_path($relativeDirectory);
if (!is_dir($directory) && !mkdir($directory, 0775, true) && !is_dir($directory)) {
throw new RuntimeException("Category upload directory could not be created: {$directory}");
}
if (!is_writable($directory)) {
throw new RuntimeException("Category upload directory is not writable: {$directory}");
}
$fileName = $file->hashName();
$file->move($directory, $fileName);
return "{$relativeDirectory}/{$fileName}";
}
public function getPublished() public function getPublished()
{ {
if ($this->get('published') == 'true') { if ($this->get('published') == 'true') {

View File

@@ -3,10 +3,11 @@
namespace App\Http\Requests\Dashboard\Category; namespace App\Http\Requests\Dashboard\Category;
use App\Models\Category; use App\Models\Category;
use App\Support\Uploads;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use RuntimeException;
class Update extends FormRequest class Update extends FormRequest
{ {
@@ -52,12 +53,31 @@ class Update extends FormRequest
{ {
if ($this->hasFile('image')) { if ($this->hasFile('image')) {
Storage::disk('local')->delete($category->image); Storage::disk('local')->delete($category->image);
return Uploads::store($this->file('image'), 'uploads/categories', 'local'); return $this->storeCategoryImage($this->file('image'));
} }
return $category->image; return $category->image;
} }
private function storeCategoryImage(UploadedFile $file): string
{
$relativeDirectory = 'uploads/categories';
$directory = public_path($relativeDirectory);
if (!is_dir($directory) && !mkdir($directory, 0775, true) && !is_dir($directory)) {
throw new RuntimeException("Category upload directory could not be created: {$directory}");
}
if (!is_writable($directory)) {
throw new RuntimeException("Category upload directory is not writable: {$directory}");
}
$fileName = $file->hashName();
$file->move($directory, $fileName);
return "{$relativeDirectory}/{$fileName}";
}
public function getFilterPower() public function getFilterPower()
{ {
if ($this->get('is_filter_power') == 'true') { if ($this->get('is_filter_power') == 'true') {

View File

@@ -147,6 +147,10 @@ class Category extends Model
public function getImage(): string public function getImage(): string
{ {
if (!in_array($this->image, ['null', null])) { if (!in_array($this->image, ['null', null])) {
if (Str::startsWith($this->image, 'uploads/categories/')) {
return asset($this->image);
}
return Storage::url($this->image); return Storage::url($this->image);
} }