Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

Can Update Model After Changing Model Name and Everyuthing Related to it.

I have a module in my Laravel project called PIBS (Pre Incident Building Surveys). It consisted of three models: Pibs, PibsContact and PibsFeature. The name was bothering me, so I decided to rename everything from "PIBS" to "Preplan." I spent hours using the search and replace to rename everything. I renamed the tables, the models, the classes, the migrations... everything. I am 99% sure I have everything renamed accordingly.

But now, when I go to update a record using the method described below, it passes validation,

    public function update(Request $request, Preplan $preplan)
    {
        
        $formFields = $request->validate([
            'addr_number' => ['required'],
            'addr_street'  => ['required'],
            'addr_city'  => ['required'],
            'addr_state'  => ['required'],
            'addr_postalcode'  => ['required'],
            'addr_lat'  => ['nullable'],
            'addr_lng'  => ['nullable'],
            'occ_name'  => ['nullable'],
            'occ_width'  => ['nullable'],
            'occ_length' => ['nullable'],
            'occ_height' => ['nullable'],
            'occ_type'  => ['nullable'],
            'occ_cons_type'  => ['nullable'],
            'cis'  => ['nullable'],
            'fireflow_25'  => ['nullable'],
            'fireflow_50'  => ['nullable'],
            'fireflow_75'  => ['nullable'],
            'fireflow_100' => ['nullable'],
            'assess_lifesafety'  => ['nullable'],
            'assess_fire'  => ['nullable'],
            'assess_const'  => ['nullable'],
            'basement'  => ['nullable'],
            'above_grade' => ['nullable'],
            'below_grade' => ['nullable'],
            'status' => ['required'],
            'day_pop'  => ['nullable'],
            'night_pop' => ['nullable'],
            'exp_sep'  => ['nullable'],
            'bldg_access' => ['nullable'],
            'occ_load'  => ['nullable'],
            'occ_mob'  => ['nullable'],
            'occ_alarm'  => ['nullable'],
            'occ_warn'  => ['nullable'],
            'tags'  => ['nullable'],
            'pub_date'  => ['nullable', 'date']
            
        ]);
      
        if (!empty($request->pub_date)) {
            $formFields['pub_date'] = Carbon::parse($request->post_date)->format('Y-m-d H:i');
        } else {
            $formFields['pub_date'] = null;
        }

        $preplan->update($formFields);
    
        return redirect()->route('preplans.index')->with('message', 'The Preplan updated successfully!');;
    }

If I do a die/dump I see the request fields as expected. I also see the newly created $formFields and its values populated correctly, but when I go to submit the data for the update using:

        $preplan->update($formFields);

it skips the update and goes to the redirect, passing me the message that the record was updated (but it wasn't)

Clearly, I must have missed something in my transition process or I need to reset something. Any idea what could be wrong?

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you have renamed everything correctly, but there might be an issue with the update method. Here are a few things you can check:

  1. Make sure that the $fillable property is correctly set in your Preplan model. The $fillable property specifies which attributes can be mass-assigned. If you have renamed any columns in the database, make sure to update the corresponding attribute names in the $fillable property.

  2. Check if the update method is actually being called. You can add a dd('update method called') statement before the $preplan->update($formFields) line to see if it gets executed. If it doesn't, there might be an issue with the routing or the form submission.

  3. Verify that the $formFields array contains the correct values. You can use dd($formFields) to inspect the array and make sure that the values are being passed correctly.

  4. Check if there are any validation errors that prevent the update from happening. You can add a dd($request->validated()) statement after the $formFields validation to see if any validation errors occur. If there are validation errors, the update method will not be called.

Here's an updated version of the update method with some additional debugging statements:

public function update(Request $request, Preplan $preplan)
{
    $formFields = $request->validate([
        // validation rules
    ]);

    // Debugging statements
    dd('update method called');
    dd($formFields);
    dd($request->validated());

    // Update the model
    $preplan->update($formFields);

    return redirect()->route('preplans.index')->with('message', 'The Preplan updated successfully!');
}

By adding these debugging statements, you can narrow down the issue and identify any potential errors.

Talinon's avatar

@phayes0289 This could be one of many things, but my first suspicion might be Route Model Binding. When you renamed everything, did you rename the parameter names in the routes?

Here's what I'm thinking.. if you still have a route for your update endpoint defined such as:

Route::patch('pibs/{pib}', [PreplanController::class, 'update']);

When Laravel tries to bind {pib} to the update() method on the controller, it's not going to find it because you have it defined as $preplan (unless you specifically configured this in the Route Service Provider). As a result, Laravel will serve up a "new" Preplan model in memory. Then when you try to update it, essentially an empty model, it doesn't throw an error (it would just return false) and deceivingly looks like your commit was successful.

Try inspecting $preplan at the top of your method and see if its the actual model or if it's just empty.

If this is the case, all you need to do to fix it is rename your route parameter to:

Route::patch('pibs/{preplan}', [PreplanController::class, 'update']);

Please or to participate in this conversation.