skip to Main Content

Good afternoon. I have a count that seems to work but it’s not visible in the blade.

In the Controller:

    public function render()
    {
        $projekte = Project::orderBy('id','desc')->paginate(10);
        $count = Project::withCount('games')
            ->get();

        //dd($count);

        return view('livewire.projects.project-overview', compact ('projekte','count'));
    }

in the blade

@foreach($projekte as $project)
<p>{{$project->games_count}} </p>
@endforeach

If I DD the $games I can see the correct amount.

DD

Inspect

I don’t understand what’s wrong.

2

Answers


  1. Chosen as BEST ANSWER

    The answer from ceejayoz was the correct one. You probably want just Project::orderBy('id','desc')->withCount('games')->paginate(10). The entire $count = ... line can go. $projekte doesn't know about $count, so the count won't be in your foreach. – ceejayoz 27 mins ago


  2. You are printing the verifiable "$count" and accessing the project variable in the view file.

    You can act in this manner.

    $projekte = Project::withCount("games")->latest()->paginate(10);
    

    After that, you can access the file in your view as follows.

    @foreach( $projekte as $project )
        <p>{{ $project->games_count }}</p>
    @endforeach
    

    Happy Coding!…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search