cristianhu06 / laravelv9x

Repositorio
2 stars 0 forks source link

CRUD Cargos #14

Open cristianhu06 opened 1 year ago

cristianhu06 commented 1 year ago

Crear MVC

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;

class Cargo extends Model { use HasFactory;

public $timestamps = true;

protected $table = 'cargos';

protected $fillable = ['nombre'];

}


- [x] Vista 

1.    index.blade.php  
2.    modals.blade.php
3.    view.blade.php

- [x] Controlador

<?php

namespace App\Http\Livewire;

use Livewire\Component; use Livewire\WithPagination; use App\Models\Cargo;

class Cargos extends Component { use WithPagination;

protected $paginationTheme = 'bootstrap';
public $selected_id, $keyWord, $nombre;

public function render()
{
    $keyWord = '%'.$this->keyWord .'%';
    return view('livewire.cargos.view', [
        'cargos' => Cargo::latest()
                    ->orWhere('nombre', 'LIKE', $keyWord)

                    ->paginate(10),
    ]);
}

public function cancel()
{
    $this->resetInput();
}

private function resetInput()
{
    $this->nombre = null;

}

public function store()
{
    $this->validate([
    'nombre' => 'required',
    ]);

    Cargo::create([
        'nombre' => $this->nombre

    ]);

    $this->resetInput();
    $this->dispatchBrowserEvent('closeModal');
    session()->flash('message', 'Cargo Creado Correctamente.');
}

public function edit($id)
{
    $record = Cargo::findOrFail($id);
    $this->selected_id = $id;
    $this->nombre = $record-> nombre;
}

public function update()
{
    $this->validate([
    'nombre' => 'required',
    ]);

    if ($this->selected_id) {
        $record = Cargo::find($this->selected_id);
        $record->update([
        'nombre' => $this-> nombre

        ]);

        $this->resetInput();
        $this->dispatchBrowserEvent('closeModal');
        session()->flash('message', 'Cargo Successfully updated.');
    }
}

public function destroy($id)
{
    if ($id) {
        Cargo::where('id', $id)->delete();
    }
}

}

cristianhu06 commented 1 year ago

el modelo vista controlador esta listo