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

Arcylisz's avatar

Does not update data

Framework: Laravel, Inertia, Vue, Ziggy. Something went wrong, it doesn't update the data, I can see the data from the form. When I changed the value in the form and then submitted and redirected to player.index and the flash said "player updated". Validation works. What am I missing?

Route:list PUT|PATCH...player/{player}...player.update › PlayerSeasonController@update

Status Code: 303 See Other, Request URL:
http://127.0.0.1:8000/player/1
Request Method:
PUT

web.php

Route::resource('/player', PlayerSeasonController::class);

PlayerSeasonController.php

    public function update(Request $request, PlayerSeason $playerSeason)
    {
        $playerSeason->update(
            $request->validate([
                'name' => 'min:3|max:32|string',
                'damage' => 'required|integer|min:0'
            ])
        );
        return redirect()->route('player.index')
            ->with('success', 'gracz zaktualizowany');
    }

    public function edit(Request $request, $player_id)
    {
        $player = PlayerSeason::with('user', 'guild')->find($player_id);
        return inertia('Player/Edit',
            [
                'player' => $player
            ]);
    }

Edit.vue

<template>
    <div class="grid container mx-auto p-4 place-items-center border-2 border-sky-500 rounded-md ">
        <p>Info player:</p>
        <p>Username: {{ player.user.name }}</p>
        <p>Guild: {{ player.guild.name }}</p>
        <p>ID user: {{ player.user.id }}</p>
        <p>ID player: {{ player.id }}</p>
    </div>
    <form class="grid grid-cols-6 grid-cols-subgrid h-screen justify-center" @submit.prevent="update" >
    <div class="text-center">
        <div>
            <label class="label">Username</label>
            <input v-model.number="form.name" type="text" class="text-center shadow-md" />
            <div v-if="form.errors.name">
                {{ form.errors.name }}
            </div>
        </div>

        <div>
            <label class="label">Damage</label>
            <input v-model.number="form.damage" type="text" class="text-center shadow-md" />
            <div v-if="form.errors.damage">
                {{ form.errors.damage }}
            </div>
        </div>
        <div>
            <button class="btn mt-3" type="submit">Update</button>
        </div>
    </div>
    </form>
</template>

<script setup>
import { useForm } from '@inertiajs/vue3'
import { route } from "ziggy-js";

const props = defineProps({
    player: Object
})

const form = useForm({
    name: props.player.user.name,
    damage: props.player.damage,
})

const update = () => {
    form.put(route('player.update', { player: props.player.id }))
}
</script>
0 likes
9 replies
gych's avatar

Did you add name and damage to $fillable in your PlayerSeason Model?

   protected $fillable = [
        'name',
        'damage'
    ];
Arcylisz's avatar

@gych Model: PlayerSeason

protected $fillable = ['player_id', 'season_id', 'damage', 'is_observer', 'is_star', 'guild_id', 'updated_by'];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'player_id');
    }

Model: User

    protected $fillable = [
        'name',
        'email',
        'password',
    ];

I tested adding 'name' in the PlayerSeason model and nothing happens the update

gych's avatar

@Arcylisz If you also want to update the name column from PlayerSeason the attribute name should be added to $fillable

Does PlayerSeason database table contain the name column?

rianmca's avatar

You logic its not check data updated or not, even fail it will return. If you want to check you can pass as variable like this:

$affected = $playerSeason->update(
        $request->validate([
            'name' => 'min:3|max:32|string',
            'damage' => 'required|integer|min:0'
        ])
    );
if($affected){
// do it some if success
}else{
// dot it fail
}

The important thing is you set $fillable in your model $name and $damage

Arcylisz's avatar

@rianmca well, public function update(Request $request, PlayerSeason $playerSeason) { $validatedData = $request->validate([ 'name' => 'min:3|max:32|string', 'damage' => 'required|integer|min:0' ]);

    $affected = $playerSeason->update($validatedData);

    if ($affected) {
        return redirect()->route('player.index')
            ->with('success', 'player has been updated.');
    } else {
        return dd($playerSeason);
    }
}

This results in an error,

App\Models\PlayerSeason {#1382 ▼ // app\Http\Controllers\PlayerSeasonController.php:42
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  +usesUniqueIds: false
  #hidden: []
  #visible: []
  #fillable: array:8 [▶]
  #guarded: array:1 [▶]
}

I forgot about log from storage.

[2024-02-26 10:55:27] local.ERROR: Method App\Http\Controllers\PlayerSeasonController::renderable does not exist. {"userId":1,"exception":"[object] (BadMethodCallException(code: 0): Method App\Http\Controllers\PlayerSeasonController::renderable does not exist. at C:\Users\Administrator\PhpstormProjects\AbexGuilds\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:68)
[stacktrace]

My project: https://github.com/olek1305/AbexGuilds

jaseofspades88's avatar

I believe your problem lies in your route model binding, @Arcylisz. The dump above proves this because the model has not been hydrated correctly. This is a common issue when the name of the variable, in your case $playerSeason doesn't match the route definition of $player in your route definition.

If you change $playerSeason to $player or vice versa, the model will be fetched correctly and the update should work.

2 likes
Arcylisz's avatar
Arcylisz
OP
Best Answer
Level 1

@jaseofspades88 I fixed this: Before:

    public function update(Request $request, PlayerSeason $playerSeason)
    {

        $validatedData = $request->validate([
            'name' => 'min:3|max:32|string',
            'damage' => 'required|integer|min:0'
        ]);

        $affected = $playerSeason->update($validatedData);

        if ($affected) {
            return redirect()->route('player.index')
                ->with('success', 'player has been updated.');
        } else {
            return dd($playerSeason);
        }
    }

After:

    public function update(Request $request, $player_id)
    {
        $playerSeason = PlayerSeason::findOrFail($player_id);

        $validatedData = $request->validate([
            'name' => 'min:3|max:32|string',
            'damage' => 'required|integer|min:0|max:100000000'
        ]);

        if (isset($validatedData['name'])) {
            $playerSeason->user()->update(['name' => $validatedData['name']]);
            unset($validatedData['name']);
        }

        $affected = $playerSeason->update($validatedData);
        
        return redirect()->route('player.index')
            ->with('success', 'Player has been updated.');
    }

I think i messed up with tabels SQL and i got rid of this PlayerSeason $playerSeason to $player_id. name is from users and damage is from player_seasons

jaseofspades88's avatar

In ignoring the solution and removing route model binding, you've made your controller method messy and it has an unnecessary database call, then you mark those changes as the answer for the original question? Stay classy, @Arcylisz. More people will be queueing up to give their free time to help you...

1 like

Please or to participate in this conversation.