brandonx
brandonx

Reputation: 248

Trying to update a DB entry throws "A four digit year could not be found Data missing" exception

I am working through creating a CRUD app in Laravel, I am using the default created_at and updated_at columns that seem to populate automatically when I insert or update to the database. When changing from using the edited entry's ID to pulling in the actual model entry as a parameter in my update method, I stumbled upon the exception "InvalidArgumentException A four digit year could not be found Data missing" when calling $installer->update(['my field 1', ...]);

I have tried dd'ing my request and the $installer object that is pulled in and everything looks correct, but updating this way seems to break the update on the updated_at field. Below are the relevant snippets:

My model class:

class Installer extends Model
{
    protected $guarded = [];
}

My update method:

    public function update(Installer $installer) {
        request()->validate([
            'FirstName' => 'required',
            'LastName' => 'required',
            'Position' => 'required',
            'Status' => 'required',
            'EmpId' => 'required'
        ]);
        $installer->update(['FirstName', 'LastName', 'Position', 'Status', 'EmpId']);

        return redirect('/installers');
    }

The relevant part of my edit form:

    <form method="POST" action="/installers/{{ $installer->id }}" style="margin-bottom: 1em">
        @method('PATCH')
        @csrf
        <div class="field">
            <label class="label" for="FirstName">First Name</label>
            <div class="control">
                <input type="text" class="input" name="FirstName" placeholder="First Name" value="{{ $installer->FirstName }}" required>
            </div>
        </div>
        <div class="field">
            <label class="label" for="LastName">Last Name</label>
            <div class="control">
                <input type="text" class="input" name="LastName" placeholder="Last Name" value="{{ $installer->LastName }}" required>
            </div>
        </div>
        <div class="field">
            <label class="label" for=Position">Position</label>
            <div class="control">
                <input type="text" class="input" name="Position" placeholder="Position" value="{{ $installer->Position }}" required>
            </div>
        </div>
        <div class="field">
            <label class="label" for="Status">Status</label>
            <div class="control">
                <input type="text" class="input" name="Status" placeholder="Status" value="{{ $installer->Status }}" required>
            </div>
        </div>
        <div class="field">
            <label class="label" for="EmpId">Employee ID</label>
            <div class="control">
                <input type="text" class="input" name="EmpId" placeholder="Employee ID" value="{{ $installer->EmpId }}" required>
            </div>
        </div>
        <div class="field">
            <div class="control">
                <button type="submit" class="button is-link">Update Installer</button>
            </div>
        </div>
    </form>

How do I modify this so that eloquent will continue to update the updated_at field automatically?

Upvotes: 0

Views: 226

Answers (3)

Mohammad Hosseini
Mohammad Hosseini

Reputation: 1807

Your update method is wrong

One easy way is try something like below:

public function update(Installer $installer) {
    $validatedData = request()->validate([
        'FirstName' => 'required',
        'LastName' => 'required',
        'Position' => 'required',
        'Status' => 'required',
        'EmpId' => 'required'
    ]);
    $installer->update($validatedData); //or $installer->update([$validatedData]);

    return redirect('/installers');
}

hope this is helpful

Upvotes: 1

brandonx
brandonx

Reputation: 248

So I just made a syntax error but I figured I would leave this up if anyone has a similar issue. In my update method:

        $installer->update(['FirstName', 'LastName', 'Position', 'Status', 'EmpId']);

should read:

        $installer->update(request(['FirstName', 'LastName', 'Position', 'Status', 'EmpId']));

Upvotes: 0

You are not including an array to the update method

Must be something like this:

$installer->update([
    'FirstName' => $request->FirstName,
    'LastName' => $lastNameVar,
    'ColName' => $colValue,
   ...
]);

Where $request is the data from the

Upvotes: 0

Related Questions