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

my_name_is_dimitris's avatar

Using dynamic table head in order to sync with data in cell

Im trying to build a small (offline,without the use of Stripe etc) membership app. What i want to achieve is having years as thead in a table and as table data/cell a true or false statement (if member has paid subscription). Im stuck for a few days now on how i can sync the data of table head to table cells.

in order to do that i have this method:

public function index()
    {

        $period = range(Carbon::now()->year, 2018);
        $users = User::all();

 $dates = Subscription::first()->user->subscriptions->pluck('SubscriptionYear')->toArray();

        return view('index')->with(compact('period', 'users', 'dates'));
    }

and this part of the view:



  <thead>
                        <tr>

                            <th> Name </th>
                            @foreach ($period as $date)

                            <th> {{ $date }} </th>
                            @endforeach


                        </tr>
                    </thead>
                    <tbody>
                        <tr>

                            @foreach ($users as $user)

                            <td>{{ $user-> name }}</td>
                            @endforeach



                            @foreach ($dates as $year)


                            @if ($year === $date)
                            <td> {{ 'true' }}</td>



                            @else

                            <td>{{ 'false' }} </td>

                            @endif


                            @endforeach


                        </tr>

                    </tbody>

The outcome of this is every cell shows false except the last one. I have stored fake years from 2021 back to 2018 into the database.

I think im taking the wrong route on this one. Anyone who done something similar or having a suggestion on how to achive that?

0 likes
3 replies
SilenceBringer's avatar
Level 55

@my_name_is_dimitris not sure I understand correctly what you want, but possible something like this:

    public function index()
    {

        $period = range(Carbon::now()->year, 2018, -1);
        $users = User::with('subscriptions')->all();

        return view('index')->with(compact('period', 'users'));
    }
<thead>
    <tr>
        <th> Name </th>
        @foreach ($period as $year)
            <th> {{ $year}} </th>
        @endforeach
    </tr>
</thead>
<tbody>
    @foreach ($users as $user)
        <tr>
            <td>{{ $user->name }}</td>
            @foreach ($period as $year)
                <td>{{ $user->subscriptions->first(fn($subscription) => $subscription->SubscriptionYear == $year) ? 'true' : 'false' }}</td>
            @endforeach
        </tr>
    @endforeach
</tbody>
my_name_is_dimitris's avatar

Thank you for your time.

i Get " BadMethodCallException Call to undefined method Illuminate\Database\Eloquent\Builder::all() " error .

Please or to participate in this conversation.