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')