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

lukeboy_2002's avatar

Scorelist

Hello,

I want to make a scoreless for a website. Only members can get points at a play day. Who's not present get 0 points and the fist to lose get 1 point etc etc. Now I made a score list but I can get it to work.

My route:

Route::get('/scores', [\App\Http\Controllers\Admin\ScoreController::class, 'index'])->name('scores.index');
Route::post('/scores/{score}', [\App\Http\Controllers\Admin\ScoreController::class, 'update'])->name('scores.update');

In my model user I added:

    public function score():hasOne
    {
        return $this->hasOne(Score::class);
    }

    public function playedGames():hasOne
    {
        return $this->hasOne(Score::class);
    }

in my model Score

    protected $fillable = [
        'user_id',
        'played_games',
        'score',
    ];

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

ScoreController

     */
    public function index()
    {
        $members = User::role('member')
            ->orderBy('username')
            ->with('score', 'playedGames', 'roles')
            ->get();

        return view('admin.scores.index', compact('members'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Score $score)
    {

        $userid = $request->input('user_id');
        $score = $request->input('score');

        $user = User::find($userid);

        if (!$user) {
            abort(404);
        }

        $user->score->score = $score;
        $score->save();

        return redirect()->route('admin.scores.index');
    }

in admin/score/index.blade.php

<x-admin-layout>

    <x-slot name="header">
        Scorelist
    </x-slot>

    @if (!$members->isEmpty())
        <x-card.default>
            <div class="relative overflow-x-auto shadow-md">
                <table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
                    <thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
                    <tr>
                        <th scope="col" class="px-6 py-3"></th>
                        <th scope="col" class="px-6 py-3">
                            username
                        </th>
                        <th scope="col" class="px-6 py-3">
                            Games
                        </th>
                        <th scope="col" class="px-6 py-3">
                            Points
                        </th>
                        <th scope="col" class="px-6 py-3 flex justify-end space-x-4">
                            Actions
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($members as $member )
                        <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600">
                            <th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                <img src="{{ asset('storage/' . $member->image) }}" alt="{{ $member->username }}" class="h-10 w-auto" >
                            </th>
                            <th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{ $member->username }}
                            </th>
                            <th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{ $member->playedGames ? $member->playedGames->playedGames : 0 }}
                            </th>
                            <th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{ $member->score ? $member->score->score : 0 }}
                            </th>
                            <td class="px-6 py-4 flex justify-end space-x-4">
                                <x-link.btn-primary href="#" class="px-2.5 py-2.5 text-xs font-medium"><i class="fa-solid fa-pen-to-square"></i></x-link.btn-primary>
                                <x-button.danger type="button" wire:click="deleteId({{ $member->id }})" class="px-2.5 py-2.5 text-xs font-medium"><i class="fa-solid fa-trash-can"></i></x-button.danger>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </x-card.default>

        <x-card.default class="mt-4">
            <!-- Form to update scores -->
            <form action="{{ route('admin.scores.update', $score) }}" method="POST" class="space-y-6">
                @csrf
                @method('PATCH')
                <x-form.label for="member_id" value="Select member" />
                <x-form.select name="member_id" id="member_id">
                    @foreach ($members as $member)
                        <option value="{{ $member->id }}">{{ $member->username }}</option>
                    @endforeach
                </x-form.select>
                <x-form.input-error for="member_id" class="mt-2" />

                <x-form.label for="score" value="Score" />
                <x-form.input type="number" name="score" id="score" required  />
                <x-form.input-error for="score" class="mt-2" />

                <x-button.primary class="px-3 py-2 text-xs font-medium">Save</x-button.primary>
            </form>
        </x-card.default>

    @else
        <x-card.default>
            <div class="flex flex-col justify-center items-center h-40 space-y-4">
                <div class="text-orange-500"><i class="fa-regular fa-circle-xmark fa-2xl"></i></div>
                <p class="text-xl font-bold tracking-tight text-gray-700 dark:text-white">No records found</p>
            </div>
        </x-card.default>
    @endif
</x-admin-layout>

Now th eindex part works I get only the members and how many points they have. When I select a member and add points to it it fails.

I get some different messages: Undefined variable $score when I remove variable $score in the form action: the Missing required parameter for [Route: admin.scores.update] [URI: admin/scores/{score}] [Missing parameter: score]. I tried a lot when there is a message dat de method ('PATCH') is not allowed with POST.

methode('patch')

0 likes
1 reply
LaryAI's avatar
Level 58

From your description, it seems like you're trying to update the score of a user, but you're encountering some issues. Here are some suggestions to fix your problem:

  1. In your ScoreController, you're trying to update the score of a user, but you're not fetching the score correctly. You're overwriting the $score variable with the score value from the request. Here's how you can fix it:
public function update(Request $request, Score $score)
{
    $userid = $request->input('user_id');
    $newScore = $request->input('score');

    $user = User::find($userid);

    if (!$user) {
        abort(404);
    }

    $user->score->score = $newScore;
    $user->score->save();

    return redirect()->route('admin.scores.index');
}
  1. In your blade file, you're trying to pass $score to the route, but it's not defined. You should pass the id of the user to the route instead:
<form action="{{ route('admin.scores.update', $member->id) }}" method="POST" class="space-y-6">
  1. In your routes file, you're expecting a Score model, but you're passing a user id. You should expect a user id instead:
Route::post('/scores/{user}', [\App\Http\Controllers\Admin\ScoreController::class, 'update'])->name('scores.update');
  1. In your ScoreController, you should fetch the user by the id passed in the route:
public function update(Request $request, User $user)
{
    $newScore = $request->input('score');

    if (!$user) {
        abort(404);
    }

    $user->score->score = $newScore;
    $user->score->save();

    return redirect()->route('admin.scores.index');
}
  1. Finally, you should change the method of your form to POST and remove the @method('PATCH') directive:
<form action="{{ route('admin.scores.update', $member->id) }}" method="POST" class="space-y-6">

These changes should fix your problem.

1 like

Please or to participate in this conversation.