@timiade is $state array? You need to access array with [] so $state->name will be $state['name'].
Oct 29, 2022
4
Level 7
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
Please or to participate in this conversation.