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/Podcasts.php
<?php

namespace App\Http\Livewire;

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

class Podcasts extends Component{

    use WithPagination;

    protected $paginationTheme = 'bootstrap';

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

    public $search='';

    public $podcast_id,$titulo,$podcast;

    public $updateMode = 0;

    public function render(){

        $podcasts = Podcast::where('titulo','LIKE','%'.$this->search.'%')->paginate(20);

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

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

    public function store(){

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

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

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

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

        $this->resetInputFields();

    }

    public function edit($id){

        $this->updateMode = 2;
        $podcast = Podcast::find($id);
        $this->podcast_id = $id;
        $this->titulo = $podcast->titulo;     
        $this->emit('podcast', $podcast->podcast); 
    }

    public function crear(){

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

    }

    public function cancel(){

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

    }

    public function update(){

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

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

        }
    }

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

            $podcast = Podcast::find($id);

            $podcast->delete();

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

}