HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/8.0.30
System: Linux multiplicar 3.10.0-1160.102.1.el7.x86_64 #1 SMP Tue Oct 17 15:42:21 UTC 2023 x86_64
User: root (0)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: /var/www/html/xfacil.desafio.com.py/app/Http/Livewire/Boletines.php
<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Boletin;
use App\Models\User;
use App\Notifications\NuevaNotificacion;
use Auth;

class Boletines extends Component{

    use WithPagination;

    protected $paginationTheme = 'bootstrap';

    protected $queryString = ['search' => ['except' => '']];

    public $search='';

    public $boletin_id,$titulo,$boletin;

    public $updateMode = 0;

    public function render(){
        $boletines = Boletin::where('titulo','LIKE','%'.$this->search.'%')->paginate(20);

        return view('livewire.boletines.index',["boletines"=>$boletines]);
    }

    private function resetInputFields(){
        $this->boletin_id = '';
        $this->titulo = '';
        $this->emit('boletin', '');
    }

    public function store(){

        $validatedDate = $this->validate([
            'titulo' => 'required',
            'boletin' => 'required'
        ],
        [
            'titulo.required' => 'El campo Titulo es requerido',
            'boletin.required' => 'El campo boletin es requerido',
        ]);

        $post = Boletin::create([
            'titulo' => $this->titulo,
            'boletin' => $this->boletin,
        ]);

        User::all()
            ->each(function(User $user) use ($post){
                $user->notify(new NuevaNotificacion([
                    'url' => '/e-learning/boletinesitae?search='.$post->titulo,
                    'titulo' => $post->titulo,
                    'descripcion' => 'Nuevo Boletin ITAE agregado',
                ]));
            });

        $this->emit('alert', ['type' => 'success', 'message' => 'boletin agregado correctamente!']);

        $this->resetInputFields();

    }

    public function edit($id){

        $this->updateMode = 2;
        $boletin = Boletin::find($id);
        $this->boletin_id = $id;
        $this->titulo = $boletin->titulo;     
        $this->emit('boletin', $boletin->boletin); 
    }

    public function crear(){

        $this->updateMode = 1;
        $this->resetInputFields();

    }

    public function cancel(){

        $this->updateMode = 0;
        $this->resetInputFields();

    }

    public function update(){

        $validatedDate = $this->validate([
            'titulo' => 'required',
            'boletin' => 'required'
        ],
        [
            'titulo.required' => 'El campo Titulo es requerido',
            'boletin.required' => 'El campo boletin es requerido',
        ]);

        if ($this->boletin_id) {
            $boletin = Boletin::find($this->boletin_id);
            $boletin->update([
                'titulo' => $this->titulo,
                'boletin' => $this->boletin,
            ]);
            $this->updateMode = 0;
            $this->emit('alert', ['type' => 'info', 'message' => 'Boletin actualizado correctamente!']);
            $this->resetInputFields();

        }
    }

    public function delete($id){
        if($id){

            $boletin = Boletin::find($id);

            $boletin->delete();

            $this->emit('alert', ['type' => 'error', 'message' => 'Boletin eliminado correctamente!']);
        }
    }

}