Php Folio Volt

mail@pastecode.io avatar
unknown
php
a year ago
1.6 kB
6
Indexable
<?php

use function Livewire\Volt\{state,computed};
use App\Models\Todo;

state(description: '');

$addTodo = function (){
    Todo::create(['description'=> $this->description]);
    $this->todos = Todo::where('description', 'like', '%'.$this->search.'%')->get();
};

state('search')->url();

$todos = computed(function () {
    return Todo::where('description', 'like', '%'.$this->search.'%')->get();
});

$delete = function ($id){
   Todo::where('id',$id)->delete();

};

$searchFunc =  function ($value){
    $this->search = $value;
    $this->todos = Todo::where('description', 'like', '%'.$value.'%')->get();
}
?>

<html>
<head>
    <title>Todos</title>
</head>
<body>
@volt
<div>
    <h1>Add Todo</h1>
    <span>Search: {{$this->search}}</span>
    <p>Keydown Enter</p>
    <div><input  wire:keydown.enter="searchFunc($event.target.value)" type="search" placeholder="Search posts by description..."  value="{{$this->search}}"></div>
    <p>Live</p>
    <div><input wire:model.live="search" type="search" placeholder="Search posts by description..."  value="{{$this->search}}"></div>
    <br>
    <form wire:submit="addTodo">
        <input type="text" wire:model="description">
        <button type="submit">Add</button>
    </form>

    <h1>Todos</h1>
    @if(count($this->todos)==0)
        <span>Liste boş</span>
    @endif
    <ul>
        @foreach ($this->todos as $todo)
            <li wire:key="{{ $todo->id }}">{{ $todo->description }} | <button wire:click="delete({{$todo->id}})">Sil</button></li>
        @endforeach
    </ul>
</div>
@endvolt
</body>
</html>