File: /var/www/html/xfacil.desafio.com.py/app/Http/Livewire/Categoriareglamentos.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Categoriareglamento;
use App\Models\Reglamento;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use Image, file;
class Categoriareglamentos extends Component{
use WithFileUploads;
protected $queryString = ['buscar' => ['except' => '']];
protected $paginationTheme = 'bootstrap';
public $buscar='';
public $categoria_id,$nombre,$foto,$fotoupdate,$crear=0;
public function render(){
$categorias=Categoriareglamento::where('nombre','LIKE',"%{$this->buscar}%")->paginate(20);
return view('livewire.categoriareglamentos',["categorias"=>$categorias]);
}
private function resetInputFields(){
$this->categoria_id = '';
$this->nombre = '';
$this->foto = '';
$this->fotoupdate = '';
$this->crear = 0;
}
public function cancel(){
$this->resetInputFields();
}
public function crear(){
$this->crear = 1;
}
public function store() {
$validatedDate = $this->validate(
[
'nombre' => 'required',
'foto' => 'required',
],
[
'nombre.required' => 'El campo Titulo categoria es requerido',
'foto.required' => 'El imagen de portada es requerido',
]
);
$categoria=new Categoriareglamento;
$categoria->nombre=$this->nombre;
if($file = $this->foto) {
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (is_file( public_path() . '/pdf/' . $nombre )) {
$nombre = rand() . $nombre;
}else{
Image::make($this->foto)
->heighten(1000)
->save(public_path() . '/pdf/' . $nombre);
$categoria->foto=$nombre;
$control=1;
}
}
}
if ($categoria->save()){
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Categoria creada correctamente.']);
}
}
public function edit($id){
$this->resetInputFields();
$categoria=Categoriareglamento::find($id);
$this->categoria_id = $id;
$this->nombre = $categoria->nombre;
$this->fotoupdate = $categoria->foto;
$this->crear = 2;
}
public function update() {
$validatedDate = $this->validate(
[
'nombre' => 'required',
],
[
'nombre.required' => 'El campo Titulo categoria es requerido',
]
);
$categoria=Categoriareglamento::find($this->categoria_id);
$foto="";
$categoria->nombre=$this->nombre;
if(is_object($this->fotoupdate)) {
$file = $this->fotoupdate;
$foto=$categoria->foto;
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (is_file( public_path() . '/pdf/' . $nombre )) {
$nombre = rand() . $nombre;
}else{
Image::make($this->fotoupdate)
->heighten(1000)
->save(public_path() . '/pdf/' . $nombre);
$categoria->foto=$nombre;
$control=1;
}
}
}
if ($categoria->save()){
if($foto){
unlink(public_path().'/pdf/'.$foto);
}
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Categoria creada correctamente.']);
}
}
public function delete($id){
if($id){
$documentos = Reglamento::where('categoria_id',$id)->get();
foreach($documentos as $doc){
if(unlink(public_path().'/pdf/'.$doc->enlace)) {
$doc->delete();
}
}
$categoria = Categoriareglamento::find($id);
if(unlink(public_path().'/pdf/'.$categoria->foto)) {
$categoria->delete();
}
$this->emit('alert', ['type' => 'error', 'message' => '¡La categoria se elimino correctamente.!']);
}
}
}