Laravel filter users with dropdown lists, based on foreign table columns?

Viewed 26

So, I have a list of users, that I am showing on a page. I am able to list the users where it shows their attributes as well. **I want to create a dropdown list where it lets you filter the users based on the specific attribute column, for example for GradeLevel I want to list the grades and the users are then able to filter based on the grade, I want to then create a different dropdown list for Age, gender and country **.

At the moment I am getting the dropdown list values when I submit they will not filter. this has a table structure like this, all with these columns:

id
name
email
email_verified_at
password
is_admin

I also have an attributes table structure that looks like this:

id
Age
ProfileImage
Gender
Country
GradeLevel
user_id (FK of users id)

This is what my User class looks like(only relevant parts):

class User extends Authenticatable
{
public function attributes()
    {
        return $this->hasOne(Attribute::class);
    }
}

This is what the usercontroller.index:

 public function index(Request $request)
    {
        $users=User::orderBy('created_at', 'desc')
            ->get();
           // ->paginate(10);
        $usersid=User::orderBy('created_at', 'desc')->pluck('id');
        $gradeLevel = DB::table('attributes')->select('GradeLevel')->distinct()->get()->pluck('GradeLevel');

        $gradeLevel1=User::join('attributes', 'users.id','=','attributes.user_id')
            ->when(request()->input('user_id'), function ($query) {
                $query->whereColumn('user.id', request()->input('user_id'));
             })->get();

        $allAttribute = Attribute::all();

       $attribute= Attribute::whereIn('user_id',$usersid)->get();

        return view('users/all_users',compact('users','attribute', 'gradeLevel'
            ,'allAttribute'));
}

Blade view looks like this:

 {!! Form::open(['route' => 'users.index', 'method' => 'GET']) !!}

    <select name="GradeLevel" id="GradeLevel" class="form-control input-lg dynamic" data-dependent="state">
        <option value="">GradeLevel</option>


        @foreach($gradeLevel as $gradeLevels)
            <option value="{{ $gradeLevels }}">{{ $gradeLevels }}</option>
        @endforeach

    </select>

    {{ Form::Submit('submit', ['class' => 'btn btn-primary']) }}

            @if($user->attributes==null)
                <img class="profImg" src="{{ url('storage/images/default-image-620x600.jpeg') }}"/>

                <p> {{$user->name}}</p>

                <p >Grade is: ?</p>

                <p >Age is: ?</p>

                <p >Country is: ?</p>


            @endif

            @if($user->attributes!=null)

                @if($user->attributes->ProfileImage!=null)
                    <img class="profImg" src="{{ url('storage/images/'.$user->attributes->ProfileImage) }}"/>
                @else
                    <img class="profImg" src="{{ url('storage/images/default-image-620x600.jpeg') }}"/>

                @endif
                <p> {{$user->name}}</p>
                <p>Gender {{$user->attributes->Gender}}</p>
                @if($user->attributes->GradeLevel!=null)
                    <p >Grade is: {{$user->attributes->GradeLevel}}</p>
                @else
                    <p >Grade is: ?</p>
                @endif
0 Answers
Related