SimonAngatia's avatar

Laravel Model not updating.

I am facing a weird problem where Laravel Model is not updating data. I have tried using eloquent, raw queries but nothing works. It was working well but it just stopped, I don't know when. When I update, it returns a success message but no data is saved. All the fields are defined in the fillable.

here is my model:

class Jewel extends Model
{
    use SoftDeletes;
    protected $table = 'jewel';

    protected $casts = [
        'supplier_id' => 'int',
        'selected_properties'=>'array',
        'matching_item_codes'=>'array',
        'diamond_shapes'=>'array',
        'selected_tags'=>'array',
        'max_carat'=>'float',
        'min_carat'=>'float',
        'setting_type' => 'int',
        'updated_by' => 'int'
		all others
...
    ];

    protected $fillable = [
        'supplier_id',
        'discount_id',
        'item_code',
        'parent_id',
        'centerstone_id',
        'design_code',
        'product_title',
		all others
...
]

Controller Code:

public function updateJewels(Request $request, $id)
    {
        $jewelry = Jewel::find($id);
        // dd($request->all());
        $jewelry->update([
            'product_desc'=>$request->product_desc,
            'product_short_desc'=>$request->product_short_desc
            
        ]);
}

It returns a success message but no data is updated.

0 likes
13 replies
jlrdw's avatar

@SimonAngatia I meant like example in link I gave:

$flight = Flight::find(1);

$flight->name = 'Paris to London';

$flight->save();
SimonAngatia's avatar

@jlrdw I have actually tried all those. Including using raw queries but nothing is working

SilenceBringer's avatar

@simonangatia as far as really I don't see fields in the fillable array - maybe problem here?

public function updateJewels(Request $request, $id)
    {
        $jewelry = Jewel::find($id);

		$jewelry->product_desc = $request->product_desc;
		$jewelry->product_short_desc = $request->product_short_desc;
		$jewelry->save();
}

also - check column names (in migrations, model fillable fields and request data)

SimonAngatia's avatar

@SilenceBringer I have actually tried all those. Including using raw queries but nothing is working, the columns are in the fillables, it has more than 70 items so I couldn't list all of them.

SilenceBringer's avatar

@SimonAngatia it looks strange. can you show output of

public function updateJewels(Request $request, $id)
    {
		dump($request->product_desc);
        $jewelry = Jewel::find($id);
		dump($jewelry->product_desc);

		$jewelry->product_desc = $request->product_desc;
		$jewelry->save();
		dump($jewelry->product_desc);
        $oneMoreTime = Jewel::find($id);
		dump($oneMoreTime->product_desc);
}
Sinnbeck's avatar

I think the hint is here

It returns a success message

But the code you posted does not. So you are most likely hitting a different controller/method

SimonAngatia's avatar

@Sinnbeck No it is the same controller. When I try to dd($request->all()), I get the data from the request.

Sinnbeck's avatar

@SimonAngatia Ok. Strange that it returns a success message when you dont return anything..

Can you give us something to work with

public function updateJewels(Request $request, $id)
    {
        $jewelry = Jewel::find($id);
        dd($request->all(), $jewelry);
        $jewelry->update([
            'product_desc'=>$request->product_desc,
            'product_short_desc'=>$request->product_short_desc
            
        ]);
}

What does this give you?

HimanshuBankoti's avatar

Can you please check if you started DB::beginTransaction() and forgot to `DB::commit();

RussDaley's avatar

Look for any mutators on the model you're trying to update. I had a similar issue, turns out the model I was trying to update had a mutator defined. Which was changing the data I was trying to set the value to, to the same value that was already set. So it was getting updated, but it didn't look like it was.

Sorry for awaking an old post, hopefully this can save someone else a headache.

Please or to participate in this conversation.