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

vipin93's avatar

Filtering users country wise using dropdown lists

I try to filter users according to there country wise I have three different table users , profiles, and countries in my profiles table I stored users information like country_id, user_id, etc , I have relationship like

My user model

public function profile()
    {
        return $this->hasOne('Profile');
    }

My profile model
class Profile extends \Eloquent {
protected $fillable = ['user_id','store_name', 'status','type', 'timing', 'weekend', 'about', 'image', 'country_id', 'state_id', 'district_id', 'city_id', 'block_id'];

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function country()
    {
        return $this->belongsTo('Country');
    }
}

and my Country model

public function profile()
   {
     return $this->hasMany('Profile');
   }

   public function user()
   {
    return $this->belongsTo('User');
   }

and my controller 
public function filterProfile()
    {
        $countries = Country::get();

        $profiles = Profile::with('user')->where('country_id', '=' , $countries)->latest()->get();

        return View::make('users.search',compact('countries'))->withProfiles($profiles)->with('title', 'filter');
    }

and my view
@extends('layouts.master')

@section('content')


<div class="row">
 
 <div class="col-md-2">

 {{ Form::open(['method' => 'GET']) }}
   
                  <div class="form-group">
                    <lable for="country">Select Country:</lable>
                    <select name="country" id="country" class="form-control">
                      @foreach($countries as $country )
                      <option value="{{ $country->id }}">{{ $country->name }}</option>
                       @endforeach
                    </select> 
                  </div>

 {{ Form::close() }}

 </div>
 
  <div class="col-md-10">
    @if($profiles->count())
       @foreach($profiles as $user)

               
                
                    <div class="col-md-3">
                          <a href="{{ route('profile', $user->user->username) }}">
                              <h3 style="position:center;">{{ $user->store_name }}</h3>
                          </a>
                          <div class="thumbnail">
                              <a href="{{ route('profile', $user->user->username) }}">      
                             <img src="{{ asset('images/'.$user->image) }}" alt="{{ $user->title }}" class="img-rounded">
                              </a>  
                           </div>                                                       
                    </div>
                
               
            </div>
 
  @endforeach

  @else
     <p> No users found in this country. </p>
  @endif

  </div>

</div>

@stop

Actually i dont have any idea plz help me out Thanks

0 likes
14 replies
bobbybouwmann's avatar

Let's think here together. First your where statement, this is never going to work since you do a select on an array. You first get all the countries and then you do where country_id = $countries, so you compare an integer in the database with an array!

You want to get the profile where the country_id is the same as a country. So your first step would be get the correct country id. Take a look at the example:

public function filterProfile()
{
    // This is the correct country selected in the dropdown
    $country = Country::findOrFail(Input::get('country_id'); 
    
    // More stuff here  
}

The next step if to get the correct profile.

public function filterProfile()
{
    // This is the correct country selected in the dropdown
    $country = Country::findOrFail(Input::get('country_id');
    
     // Get the profile 
    $profile = Profile::with('user')->where('country_id', '=', $country->id)->get();
    
    // Return view
}

The next step is to return the correct view!

public function filterProfile()
{
    // This is the correct country selected in the dropdown
    $country = Country::findOrFail(Input::get('country_id');
    
     // Get the profile 
    $profile = Profile::with('user')->where('country_id', '=', $country->id)->get();
    
    return View::make('users.search', compact($profile));
}

Note: I only return the profile here but if you want to return more feel free to do that!

1 like
bobbybouwmann's avatar

It means that there is no record in the database! Are you sure there are countries in the database?

vipin93's avatar

@blackbird In my database profiles table i have user_id->10, country_id->1 and my countries table id->1, name->Inda and three more

vipin93's avatar

@blackbird here is my full errors

Illuminate \ Database \ Eloquent \ ModelNotFoundException 
No query results for model [Country].
Open: C:\lamp\www\flash\app\controllers\UsersController.php
        ->with('title', 'Brows Profile');
    }
 
    public function filterProfile()
    {
        //$countries = Country::get();
 
        $country   = Country::findOrFail(Input::get('country_id'));
 
        $profiles = Profile::with('user')->where('country_id', '=', $country->id)->get();
      }

it showing line no. error
. Illuminate\Database\Eloquent\Model findOrFail
…\app\controllers\UsersController.php51
bobbybouwmann's avatar

The exception is pretty clear, model not found. This means it can't find a record in the database matching your criteria.

Are you sure that Input::get('country_id') returns the correct number?

vipin93's avatar

@blackbird here one thing I want to u ask " Input::get('country_id') " what of this mean, When I hit the my route for filtering the users It giving that error And when I try to var_dump $country same error

Please or to participate in this conversation.