Open cristianhu06 opened 1 year ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class Comuna extends Model { use HasFactory;
public $timestamps = true; protected $table = 'comunas'; protected $fillable = ['nombre'];
}
- [x] Vista 1. index.blade.php 2. modals.blade.php 3. view.blade.php - [x] Controlador
namespace App\Http\Livewire;
use Livewire\Component; use Livewire\WithPagination; use App\Models\Comuna;
class Comunas extends Component { use WithPagination;
protected $paginationTheme = 'bootstrap'; public $selected_id, $keyWord, $nombre; public function render() { $keyWord = '%'.$this->keyWord .'%'; return view('livewire.comunas.view', [ 'comunas' => Comuna::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', ]); Comuna::create([ 'nombre' => $this-> nombre ]); $this->resetInput(); $this->dispatchBrowserEvent('closeModal'); session()->flash('message', 'Comuna Successfully created.'); } public function edit($id) { $record = Comuna::findOrFail($id); $this->selected_id = $id; $this->nombre = $record-> nombre; } public function update() { $this->validate([ 'nombre' => 'required', ]); if ($this->selected_id) { $record = Comuna::find($this->selected_id); $record->update([ 'nombre' => $this-> nombre ]); $this->resetInput(); $this->dispatchBrowserEvent('closeModal'); session()->flash('message', 'Comuna Successfully updated.'); } } public function destroy($id) { if ($id) { Comuna::where('id', $id)->delete(); } }
Crear MVC
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class Comuna extends Model { use HasFactory;
}
<?php
namespace App\Http\Livewire;
use Livewire\Component; use Livewire\WithPagination; use App\Models\Comuna;
class Comunas extends Component { use WithPagination;
}