Help me!
Feb 26, 2015
14
Level 13
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
Please or to participate in this conversation.