-1
There is some way to add a Count in the Blade. For in the interaction it returns 1 1 0 0
@forelse($surveys as $survey)
<p> @forelse($survey->options as $option)
{{ $option->users->count()}}
@empty
Nenhum voto até o momento
@endforelse
@empty
@endforelse
On the controller it’s like this
public function index()
{
$user = $this->user->find(auth()->user()->id);
$surveys = Survey::whereHas('options', function ($query) use ($user) {
$query->where('user_id', '=', $user->id );
})->get();
return view("painel.usuario.index",compact('surveys'));
}
A Survey has several options. One option has several user. A user can choose several options, but from different polls. An option belongs only to a sruvey.
In case it has 2 votes. but it brings one to one. for each option has only one users
Hi Fabio, please put more information in the question; 1. what the variable $Survey has?
– RFL
the variable Survey has several options and each option can have several users
– Fábio Gilberto
but the options belong to only one Survey
– Fábio Gilberto
the controller that returns that view looks like this $Surveys = Survey::whereHas('options', Function ($query) use ($user) { $query->Where('user_id', '=', $user->id ); })->get(); Return view("dashboard.usuario.index",Compact('Surveys'));
– Fábio Gilberto
Check a relationship of type hasManyThrough https://laravel.com/docs/5.8/eloquent-relationships#has-Many-through
– Jorge Costa
It works even pivoting the table option_users ? Why I tried, it didn’t work.
– Fábio Gilberto
Not one for many, one for many ?
– Fábio Gilberto
What are these options?
– Lucas Antonio