WallyJ
2 months ago
705
10
Laravel

Passing Hidden Field value back to Livewire component is Null

Posted 2 months ago by WallyJ

From my old PHP form days, I have a form within a @foreach loop that has a hidden field to capture the deal id, so I can send that on to be used by the controller.

However, if I add

wire:model="deal_id"

to the input field, the deal id no longer shows in the field and the dd($this->deal_id) shows null. I tested this with a text field instead of a hidden field so I could see the value.

How do I get the deal is from each @foreach loop sent back to the component? Because of the @foreach loop I have multiple forms on the page, but the "Add Task" button needs to add the task via the correct deal id.

For reference:

Foreach section in my component view:

@foreach($this->contacts['deals'] as $deal)
<div class="col-md-6">
        <div class="row">
            <div class="col-md-9"><h3><a href="/deals/{{$deal['id']}}">{{$deal['dealtype']}}</a></h3></div>
            <h1>DealID: {{ $deal['id'] }}</h1>
            <div class="col-md-3">
                <a href="link_to_action('[email protected]', $title, $parameters = array(), $attributes = array());">Delete</a>
            </div>
        </div>
        <div class="tasks">
            <h5>Tasks</h5>
            <div class="card-block">
            <div class="form-row">
                <div class="input-group form-group col-md-9">
                    <input type="text" name="tasktext" class="form-control" id="tasktext" placeholder="New Task" wire:model="tasktext">
                </div>
                <div>
                    <input type="text" value="{{ $deal['id'] }}" wire:model="deal_id">
                </div>
                <div class="form-group col-md-3" id="datepickerfield">
                    <div class="input-group date taskduedatepicker">
                    <input type="text" name="taskduedate" class="form-control" id="taskduedate" placeholder="Due Date" data-provide="datepicker" data-date-autoclose="true" data-date-today-highlight="true" autocomplete="off">
                    </div>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-12">
                    <input type="hidden" name="deal_id" id="deal_id" value="{{ $deal['id'] }}" wire:model="dealid">
                    <button class="btn btn-primary" wire:click="addTask" type="submit">Add</button>
                </div>
            </div>
            </div>
            <div>
                <ul class="list-group">
                    @foreach($deal['tasks'] as $task)
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <div>
                            <input type="checkbox" class="mr-4">
                            {{Carbon\Carbon::parse($task['taskduedate'])->format('m/d/Y')}} - {{$task['tasktext']}}
                        </div>
                        <div>
                            <form class=".mb-0" action="#" method="POST">
                                @csrf
                                <button class="btn btn-sm btn-danger">&times;</button>
                            </form>
                        </div>
                    </li>
                    @endforeach
                </ul>
            </div>
        </div>
</div>
@endforeach

And my controller:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Task;

class Contactshow extends Component
{

    public $contacts;
    public $tasktext;
    public $deal_id;
    public $taskduedate;

    public function mount($contacts)
    {
        $this->contacts = $contacts;
    }

    public function render()
    {
        return view('livewire.contactshow');
    }

    public function addTask()
    {
        dd($this->deal_id);
        Task::create([
            'deal_id' => $this->deal_id,
            'tasktext' => $this->tasktext,
            'taskduedate' => $this->taskduedate,
        ]);
    }
}

Please sign in or create an account to participate in this conversation.