WallyJ
3 days ago
143
18
Laravel

Data not passing in Livewire Component

Posted 3 days ago by WallyJ

I receive this error using the following view and controller:

Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'deal_id' doesn't have a default value (SQL: insert into tasks (tasktext, updated_at, created_at) values (Upload, 2020-01-14 21:27:15, 2020-01-14 21:27:15))

Controller:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Task;

class Deals extends Component
{
    public $deal;
    public $dealid;
    public $tasktext;

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

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

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

View:

<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" placeholder="New Task" wire:model="tasktext">
            </div>
            <div class="input-group form-group">
                <input type="text" name="dealid" class="form-control" value="{{ $this->deal['id'] }}" wire:model="dealid">
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-12">
                <button class="btn btn-primary" wire:click="addTask" type="submit">Add</button>
            </div>
        </div>
        </div>
        <div>
            <ul class="list-group">
                @foreach($this->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>

I believe that I have coded the "dealid" the same as the "tasktext", but it isn't seen as part of the "Task::create" function.

What am I missing?

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