category qosishsh to'g'irlandi
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace App\Http\Requests\Dashboard\Category;
|
||||
|
||||
use App\Support\Uploads;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use RuntimeException;
|
||||
|
||||
class Request extends FormRequest
|
||||
{
|
||||
@@ -48,12 +49,31 @@ class Request extends FormRequest
|
||||
public function getImage(): string
|
||||
{
|
||||
if ($this->hasFile('image')) {
|
||||
return Uploads::store($this->file('image'), 'uploads/categories', 'local');
|
||||
return $this->storeCategoryImage($this->file('image'));
|
||||
} else {
|
||||
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()
|
||||
{
|
||||
if ($this->get('published') == 'true') {
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace App\Http\Requests\Dashboard\Category;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Support\Uploads;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use RuntimeException;
|
||||
|
||||
class Update extends FormRequest
|
||||
{
|
||||
@@ -52,12 +53,31 @@ class Update extends FormRequest
|
||||
{
|
||||
if ($this->hasFile('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;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
if ($this->get('is_filter_power') == 'true') {
|
||||
|
||||
@@ -147,6 +147,10 @@ class Category extends Model
|
||||
public function getImage(): string
|
||||
{
|
||||
if (!in_array($this->image, ['null', null])) {
|
||||
if (Str::startsWith($this->image, 'uploads/categories/')) {
|
||||
return asset($this->image);
|
||||
}
|
||||
|
||||
return Storage::url($this->image);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user