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

TimiAde's avatar

Attempt to read property $field on array

i am trying to sort this query by each field using livewire

 $query = DB::table('users')
            ->join('candidates', 'users.candidate_id', '=', 'candidates.id')
            ->join('parties', 'candidates.party_id', '=', 'parties.id')
            ->join('predictions', 'users.id', '=', 'predictions.user_id')
            ->join('states', 'predictions.state_id', '=', 'states.id')
            ->where('predictions.user_prediction', '>', 50)
            ->selectRaw('users.name, candidates.name as candidate, parties.name as party, COUNT(states.state) as count')
            ->when($this->sortField, function($q){
                $q->orderBy($this->sortField, 'desc');
            })
            ->groupBy(['name', 'candidate', 'party'])
            ->paginate(30);
        
        // return $query;
        return view('livewire.winning-states-poll' , [
            'winnings' => $query
    
        ]);

My Livewire blade is

@foreach ($winnings as $each)
                    
                <tr class="bg-white text-xs border-b dark:bg-gray-800 dark:border-gray-700">
                    <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                    {{$each->name}}
                    </th>
                    <td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        {{$each->candidate}}
                    </td>
                    <td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        {{$each->party}}
                    </td>
                    <td class="py-4 px-6">
                        {{$each->count}}
                    </td>

                    <td class="py-4 px-6 w-64">
                    @php //the error is here
                        // dd($each);
						//$states is a collection of all states.
                        foreach($states as $state){
                        $array = [];

                        // dd($state);

                            if($state->name == $each->name){
                                array_push($array, $state->state);
                            }
                        }
                        echo implode(",", $array);
                    @endphp

                    </td>
                </tr>

                @endforeach

When i click the sort Button that is when the error shows

0 likes
4 replies
TimiAde's avatar

@AungHtetPaing__ the issue was the states collection i was passing it through the controller instead of the render function in livewire. I have now fixed it.

kokoshneta's avatar

You don’t try to access a property called field anywhere in the code you posted here…

TimiAde's avatar

@kokoshneta the issue was the states collection i was passing it through the controller instead of the render function in livewire. I have now fixed it.

Please or to participate in this conversation.