Search code examples
laravel-livewirelaravel-10

sending id from route to livewire (function) and got error about parameter or somethink


im new on livewire and im got stuck from it.

"i want to get 'id' from route and fetch it using livewire component (function) to get data from by id in the database, and get error about parameter 0 or somethink, here the error :

Unable to resolve dependency [Parameter #0 [ <required> $id ]] in class App\Livewire\StartExams

i know if it using laravel i can get te data, but when im using livewire got error

here my button :

<a class="py-2 text-sm my-0 mx-4 flex items-center transition-colors" title="{{ __('Quiz') }}"
    href="{{ route('start-quiz', $getExamsOnLecture->exams->id) }}"
    wire:click="mount({{ $getExamsOnLecture->exams->id }})">
    <i class="mr-2 fa-solid fa-question"></i>
    <span class="ml-1 duration-300 opacity-100 pointer-events-none">
        {{ $getExamsOnLecture->exams->name }}
    </span>
</a>

my route:

use App\Livewire\StartExams;
Route::get('/exams/{any}/start', StartExams::class)->name('start-quiz');

here the livewire controller:

namespace App\Livewire;
use App\Models\Questions;
use App\Models\QuestionsOption;
use Livewire\Component;

class StartExams extends Component
{
    public $id;
    public $questions = [];
    public $questionIndex = 0;
    public $question;
    public $answer;
    public $questionOption;

    public bool $finished = false;

    public function mount($id)
    {
        $this->id = $id;
        $this->questions = Questions::where('exam_id', $this->id)->get();

        foreach ($this->questions as $key => $questionOption) {
            $this->questionOption[$key]['options'] = QuestionsOption::where('question_id',  $questionOption->id)->get();
            // return $this->questions[$key]['options'];
        }

    }

    public function resetRadioOptions()
    {
        $this->answer = null;
    }

    public function submitQuiz()
    {
        //otw making save to database ///

        $this->questionIndex++;
        if ($this->questionIndex >= count($this->questions)) {
            $this->finished = true;
        } else {
            $this->question = $this->questions[$this->questionIndex];
            $this->resetRadioOptions();
        }
    }
    public function render()
    {
        return view('livewire.start-exams', [
            'id' => $this->id,
        ])->layout('layouts.auth');
    }
}

i thought at first it same as laravel so im ignore it and doing other file than this, then im im going to fix it so i can get the data based bny id, it got error..

im already trying searching on google and even AI but its failed, so im posting here.. can someone help me? just giving me step by step that i must fixed.

i want the id get it form id from tag route so i cane get data based by id

$this->questions = Questions::where('exam_id', $this->id)->get();

but if it like this (work, cause im put number instead id)

$this->questions = Questions::where('exam_id', '1')->get();

i can get the record, but based by $id its got error..


Solution

  • In the route you use a parameter called "any", while in the mount() method of your component you receive a parameter called "$id": these must have the same name otherwise the match can't be done
    Use    "any" and "$any"    or    "id" and "$id"