140 lines
3.4 KiB
PHP
140 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Categoria;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
use Intervention\Image\ImageManagerStatic as Img;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
class ShowCategoria extends Component
|
|
{
|
|
use LivewireAlert;
|
|
use WithFileUploads;
|
|
|
|
public $categoria_id;
|
|
public $photo;
|
|
public $img;
|
|
public $nombre;
|
|
public $descripcion;
|
|
public $editarCategoria=false;
|
|
public $nuevaCategoria=false;
|
|
|
|
|
|
protected $rules = [
|
|
'photo' =>'required',
|
|
'nombre' => 'required',
|
|
|
|
];
|
|
|
|
public function render()
|
|
{
|
|
|
|
$consulta_categoria = Categoria::get();
|
|
|
|
return view('livewire.show-categoria',[
|
|
'categorias' => $consulta_categoria,
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->validate();
|
|
|
|
if (!empty($this->photo)) {
|
|
|
|
$nombre=Str::random(10);
|
|
|
|
$img = img::make($this->photo)->orientate()->resize(1000, null, function($constraint){
|
|
$constraint->aspectRatio();
|
|
$constraint->upsize();
|
|
})->encode('webp');
|
|
|
|
$location = "photos/".$nombre.".webp";
|
|
Storage::disk('local')->put($location, $img);
|
|
$url_foto = $location;
|
|
} else {
|
|
$url_foto = '';
|
|
}
|
|
|
|
|
|
$guardar_categoria = new Categoria;
|
|
$guardar_categoria->nombre = $this->nombre;
|
|
$guardar_categoria->imagen = $url_foto;
|
|
$guardar_categoria->descripcion = $this->descripcion;
|
|
$guardar_categoria->save();
|
|
|
|
$this->alert('success', 'Categoria creada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->nuevaCategoria=false;
|
|
}
|
|
|
|
public function editar($id)
|
|
{
|
|
|
|
$consulta_categorias = Categoria::FindOrFail($id);
|
|
|
|
$this->categoria_id = $consulta_categorias->id;
|
|
$this->nombre = $consulta_categorias->nombre;
|
|
$this->descripcion = $consulta_categorias->descripcion;
|
|
$this->img = $consulta_categorias->imagen;
|
|
}
|
|
public function update()
|
|
{
|
|
|
|
if (!empty($this->photo)) {
|
|
|
|
$nombre=Str::random(10);
|
|
|
|
$img = img::make($this->photo)->orientate()->resize(1000, null, function($constraint){
|
|
$constraint->aspectRatio();
|
|
$constraint->upsize();
|
|
})->encode('webp');
|
|
|
|
$location = "photos/".$nombre.".webp";
|
|
Storage::disk('local')->put($location, $img);
|
|
$url_foto = $location;
|
|
} else {
|
|
$url_foto = $this->img;
|
|
}
|
|
|
|
|
|
Categoria::where('id', $this->categoria_id)->update([
|
|
'nombre' => $this->nombre,
|
|
'descripcion' => $this->descripcion,
|
|
'imagen' => $url_foto
|
|
]);
|
|
|
|
$this->alert('success', 'Categoria actualizada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
$this->limpiarInputCategoria();
|
|
$this->editarCategoria=false;
|
|
}
|
|
|
|
|
|
public function delete($id)
|
|
{
|
|
Categoria::where('id',$id)->delete();
|
|
|
|
$this->alert('success', 'Categoria eliminada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
|
|
|
|
public function limpiarInputCategoria()
|
|
{
|
|
$this->photo = null;
|
|
$this->nombre = '';
|
|
$this->descripcion = '';
|
|
}
|
|
|
|
}
|