'Laravel filter users based on their attributes, with a dropdown list?
So I have a list of users that I am showing on a page and they have attributes in a foreign table, linked via the user_id. What I want to do is sort by the users based on what is clicked in a dropdown list. However, I'm not sure how to do this, at the moment I am getting it where the sorting of these lists is happening in the select list, for example for the attribute age, in the dropdown list it is showing up as , [{"id":2,"user_id":21},{"id":3,"user_id":24}, {"id":4,"user_id":25}], for Country and gradelevel it looks like this as well. What I want is for the each category to be displayed in a drop downlist, like Age, Country, GradeLevel etc.
This is what the method to list all the users looks like:
public function render()
{
$users = User::where('id', '!=', Auth::user()->id)->limit(14)->latest()->get();
$gradeLevel= Attribute::orderBy('GradeLevel','desc')->select('id','user_id')->get();
$age= Attribute::orderBy('Age','desc')->select('id','user_id')->get();
$country= Attribute::orderBy('Country','desc')->select('id','user_id')->get();
$data=[
'gradeLevel' =>$gradeLevel,
'age' =>$age,
'country' =>$country
];
return view('livewire.add-friend',
compact('users', 'data'));
}
This is what the view looks like:
<form>
<label for="data">Role</label>
<select>
@foreach($data as $dat)
<option value="{{ $dat }}"> {{ $dat }} </option>
@endforeach
</select>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|