Dewa Aditya
Dewa Aditya

Reputation: 79

Eloquent update doesn't work in laravel 5.6

I want to make update function but the function doesn't work, plese help me for resolve this problem this is my code :

This is Route

Route::resource('ItemName', 'ItemNameController');

This is Controller

public function update(Request $request, ItemName $itemName)
{
    request()->validate([
        'inc'            => 'required',
        'item_name'      => 'required',
        'short_name'     => 'required',
        'definition_eng' => 'max:1000', 
        'definition_ind' => 'max:1000',
    ]);

    $itemName->update($request->all());

    return redirect('ItemName')->with('success', 'Item Name Updated Successfully');
}

This is View

<form action="{{ action('ItemNameController@update', $ItemName->id) }}" method="post">
    @csrf
    @method('PUT')
    <input class="form-control col-4" type="text" name="inc" placeholder="INC" value="{{$ItemName->inc}}">
    <input class="form-control mt-2" type="text" name="item_name" placeholder="Item Name" value="{{$ItemName->item_name}}">
    <input class="form-control mt-2" type="text" name="short_name" placeholder="Short Name" value="{{$ItemName->short_name}}">
    <input class="form-control mt-2" type="text" name="definition_eng" placeholder="English Definition" value="{{$ItemName->definition_eng}}">
    <input class="form-control mt-2" type="text" name="definition_ind" placeholder="Indonesia Definition" value="{{$ItemName->definition_ind}}">
    <input type="submit" class="btn btn-primary" value="Edit">
</form>

Value when i dump this code

ItemName {#512 ▼
#table: "tbl_item_name"
#fillable: array:5 [▼
0 => "inc"
1 => "item_name"
2 => "short_name"
3 => "definition_eng"
4 => "definition_ind"
]
#connection: "sqlsrv"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:8 [▶]
#original: array:8 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}

The Problem is update function is doesn't work, and i didn't get any error, can anyone help me?

Upvotes: 3

Views: 12791

Answers (5)

Matthew Ewan-Laws
Matthew Ewan-Laws

Reputation: 31

I encountered this problem

My update function in the controller is as follows

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Person  $person
     * @return \Illuminate\Http\Response
     */
    public function update(Person $person, Request $request)
    {
        $person->update($request->validate([
            'forename' => 'required',
            'surname' => ''
        ]));

        return redirect('/people');
    }

My routes/web.php file had the following route:

Route::put('/people/{id}', [PersonController::class, 'update']);

To fix this, the wildcard match should be the same as the model name.

Route::put('/people/{person}', [PersonController::class, 'update']);

Upvotes: 3

Nicky Kouffeld
Nicky Kouffeld

Reputation: 381

Note that the update() function responds false if your update values are the same as old record.

Upvotes: 0

maxwilms
maxwilms

Reputation: 2024

Ensure the $fillable array is set correctly on your Eloquent model.

class ItemName extends Model
{

    protected $fillable = [
        'inc',
        'item_name',
        'short_name',
        'definition_eng',
        'definition_ind',
    ];

    // ..

}

Normally Laravel will create a MassAssignmentException if you call update() and have not defined the $fillable array.

As soon as you define anything on the $fillable array Laravel will silently ignore any additional values.

Upvotes: 8

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this:

$items = ItemName::where($itemName)->update($request->all());

Upvotes: 1

Roshni hegde
Roshni hegde

Reputation: 415

Try with fill instead of update.

 $itemName->fill($request->all());
 $itemName->save();

Hope this will help you

Upvotes: 0

Related Questions