fahad shaikh
fahad shaikh

Reputation: 631

Laravel update eloquent not updating the record

I am using Laravel eloquent to update the record. The problem is the update method returns the integer equal to number of records should be updated but this update is not being reflected in database.

I inserted some records to check there is no database mismatch.

I am even using fillable method and defined every column into it.

Here is my code.

Modal

protected $fillable = [
        'id',
        'user_id',
        'ticket_no',
        'partner_id',
        'partner_user_name',
        'partner_user_email',
        'partner_user_contact',
        'contacted_for_id',
        'issue_type_id',
        'comm_mode_id',
        'opened_by_id',
        'assigned_to_id',
        'team_id',
        'priority_id',
        'opened_date',
        'tat_id',
        'resolved_date',
        'status_id',
        'description',
    ];

Controller

public function update(Request $request)
    {
        $validators = Validator::make($request->all(), [
            'assigned_to_id' => 'required',
            'team_id' => 'required',
            'resolve_date' => 'required',
            'status_id' => 'required',
            'description' => 'required',
        ]);

        if ($validators->fails()) {
            $data['result'] = false;
            $data['messages'] = $validators->errors()->first();

            return json_encode($data);
        }

        $assigned_to_id = $request->assigned_to_id;
        $team_id = $request->team_id;
        $resolve_date = $request->resolve_date;
        $status_id = $request->status_id;
        $description = $request->description;

        $ticket_row_id = $request->ticket_row_id;

        $updated = TICKET_TRACKER::where('id', $ticket_row_id)
            ->update(
                ['assigned_to_id' => $assigned_to_id],
                ['team_id' => $team_id],
                ['resolve_date' => $resolve_date],
                ['status_id' => $status_id],
                ['description' => $description]
            );
    }

I am not getting any error that's the most frustrated thing.

Upvotes: 1

Views: 11592

Answers (1)

loic.lopez
loic.lopez

Reputation: 2103

In order to make your model to be updated change:

 $updated = TICKET_TRACKER::where('id', $ticket_row_id)
            ->update(
                ['assigned_to_id' => $assigned_to_id],
                ['team_id' => $team_id],
                ['resolve_date' => $resolve_date],
                ['status_id' => $status_id],
                ['description' => $description]
            );

By:

 $updated = TICKET_TRACKER::where('id', $ticket_row_id)
       ->update([
                 'assigned_to_id' => $assigned_to_id,
                 'team_id' => $team_id,
                 'resolve_date' => $resolve_date,
                 'status_id' => $status_id,
                 'description' => $description
          ]);

In fact you need to have only one array with keys and values in order to view your model updated.

[EDIT 1]

As said in the comments in Laravel you need to take care about naming conventions,

If the name of the model is TicketTracker, the name of the table should be plural. So it will be ticket_trackers.

or if you want to have a custom name for your table you can configure in your model the $table property as follows:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TicketTracker extends Model
{
    protected $table = "other_table_name"
}

Upvotes: 4

Related Questions