Displaying specific course data in component view based on slug or ID

Hey folks, I’m stuck with a weird issue in my Laravel project. I’m trying to show info for just one course in my show-component blade file. But for some reason, it’s displaying all the courses from my database instead of the one I want.

Here’s what my ShowComponent looks like:

class DisplayCourse extends Component
{
    public $identifier;

    public function initialize($identifier)
    {
        $this->identifier = $identifier;
    }

    public function display()
    {
        $subject = Subject::where('identifier', $this->identifier)->first();
        return view('livewire.display-course', ['subject' => $subject]);
    }
}

I’ve also got relationships set up between my Syllabus, Level, and Subject models. The Syllabus model has many Levels, each Level belongs to a Syllabus, and Subjects belong to Levels.

Any ideas on why it might be showing all courses instead of just one? I’m pretty new to Laravel, so I might be missing something obvious. Thanks in advance for any help!

Hey MaxRock56! Interesting problem you’ve got there. :thinking:

Have you considered that the issue might be in your relationship setup? Sometimes when relationships aren’t quite right, Laravel can pull in more data than we expect.

Maybe try tweaking your query a bit:

$subject = Subject::with(['level.syllabus'])->where('identifier', $this->identifier)->first();

This way, you’re explicitly telling Laravel what related data to grab. It might help isolate the problem.

Also, just curious - how are you passing the identifier to the component from your route or controller? Sometimes the devil’s in those details!

Let us know if this helps or if you need more ideas. We’re all learning here, so don’t sweat it if it takes a few tries to crack this nut! :blush:

hey maxrock, sounds like a tricky one! have u checked if ur identifier is actually being passed correctly to the component? try dd($this->identifier) in ur initialize method to make sure its not empty. also, double-check ur blade file - might be looping thru all subjects by accident. good luck!

It seems like the issue might be in how you’re using the component. Make sure you’re passing the identifier correctly when you call the component in your blade file. Something like this should work:

<livewire:display-course :identifier="$courseId" />

Also, check your ‘livewire.display-course’ blade file. Ensure you’re not accidentally looping through all subjects there. It should be displaying data for a single subject, like:

<h1>{{ $subject->name }}</h1>
<p>{{ $subject->description }}</p>

If these don’t solve it, the problem might be in your route definition or the way you’re passing data to the view. Double-check those areas as well. Hope this helps!