onurzdgn's avatar

"model:" not working

Hello everyone, I am using Laravel 10 and Livewire 3. I am taking id from controller and then send to livewire component from normal blade. Everything is working to here. I am using this for find worker. Then I create a form. This form is take id automatically, then user choose the day and reason and post is(save button), my problem is start here. When I want to see id and reason with dd(), it show me id but now show reason. This is my normal blade:

    <div class="col-12">
        <div class="card">
            <div class="card-body" style="background-color: #80818c">
                <table class="table table-responsive-sm" style="table-layout:fixed;">
                    <thead>
                        <tr>
                            <td class="text-black">Ad Soyad</td>
                            <td class="text-black">Gün</td>
                            <td class="text-black">Geldi / Gelmeme Nedeni</td>
                            <td class="text-black"></td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($workers as $worker)
                            @livewire('admin.workers-calendar', ['id' => $worker->id])
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

This is my "workers-calendar" livewire component:

<div>
    {{-- Knowing others is intelligence; knowing yourself is true wisdom. --}}

    <form wire:submit="workerAbsent">
        @csrf
        <tr>
            <td>
                <p class="text-black">{{ $worker->name }} {{ $worker->surname }}</p>
            </td>
            <td>
                <input type="date" wire:model="day" id="day" class="form-control">
            </td>
            <td>
                <select wire:model="reason" name="reason">
                    <option value="0">Seçiniz</option>
                    <option value="1">Geldi</option>
                    <option value="2">İzinli</option>
                    <option value="3">Hastalık (Raporlu)</option>
                    <option value="4">Hastalık (Raporsuz)</option>
                    <option value="5">Haber Vermeden Gelmedi</option>
                    <option value="6">Yakın Vefaati</option>
                    <option value="7">Diğer</option>
                </select>
            </td>
            <td>
                <button class="btn btn-primary pull-right" type="submit">Kaydet</button>
            </td>
        </tr>
    </form>
</div>

This is my livewire controller:

<?php

namespace App\Livewire\Admin;

use App\Models\Workers;
use Livewire\Component;

class WorkersCalendar extends Component
{
    public $id, $reason;

    public function render()
    {
        $id = $this->id;

        $worker = Workers::find($id);

        return view('livewire.admin.workers-calendar', compact('worker'));
    }

    public function workerAbsent() {
        dd(
            'id '.$this->id, 
            'reason '.$this->reason
        );
    }
}
0 likes
2 replies
kokoshneta's avatar

I can see two issues with your code:

  • You’re trying to bind an input field to $day and your Livewire component has no property named $day
  • Your model is called Workers when presumably it should be called Worker (a single instance of the class = one single worker)

But those don’t affect the issue you’re describing. As far as I can tell, your workerAbsent() function should output both the ID and the reason; I can see no reason why it shouldn’t work.

onurzdgn's avatar
onurzdgn
OP
Best Answer
Level 2

I fix problem with this style. In livewire blade:

<tr>
    <form wire:submit="workerAbsent">
        @csrf
        <td>
            <p class="text-black">{{ $worker->name }} {{ $worker->surname }}</p>
        </td>
        <td>
            <input type="date" wire:model="day" name="day_{{ $worker->id }}" id="day_{{ $worker->d }}" class="form-control @error('day') border-danger @enderror">
            @error('day')
                <span style="color:pink">
                    {{ $message }}
                </span>
            @enderror
        </td>
        <td>
            <select wire:model="reason" name="reason_{{ $worker->id }}" id="reason_{{ $worker->d }}" class="form-control @error('reason') border-danger @enderror">
                <option value="0">Seçiniz</option>
                <option value="1">Geldi</option>
                <option value="2">İzinli</option>
                <option value="3">Hastalık (Raporlu)</option>
                <option value="4">Hastalık (Raporsuz)</option>
                <option value="5">Haber Vermeden Gelmedi</option>
                <option value="6">Yakın Vefaati</option>
                <option value="7">Diğer</option>
            </select>
            @error('reason')
                <span style="color:pink">
                    {{ $message }}
                </span>
            @enderror
        </td>
        <td>
            <button class="btn btn-primary pull-right" type="submit">Kaydet</button>
        </td>
    </form>
</tr>

Please or to participate in this conversation.