mercredi 25 avril 2018

Search bar with two inputs and a search button Laravel5

I'm working on a jobber search project online using Laravel 5.5. In my project I want to show jobbers who live in the same area and who perform the same service, or who live only in the same area, or who perform the same service by doing a search. I use three tables: User, Area and Service.

please I need your help.

Here is my search bar: I want to use this search bar to do it

Here is the user model

class User extends Authenticatable { use Notifiable, EntrustUserTrait;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['name', 'surname', 'email', 'phone', 
   'password','type',];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [ 'password', 'remember_token',];

public function area(): BelongsTo
{
    return $this->belongsTo(Area::class);
}

public function service(): BelongsTo
{
    return $this->belongsTo(Service::class);
}

}

Here is the service model

class Service extends Model { protected $fillable = ['category_id','name','description'];

public function category(): BelongsTo
{
    return $this->belongsTo(Category::class);
}

 public function users(): BelongsTo
{
    return $this->belongsToMany(User::class, 'service_id');
}

public function jobs() 
{
   return $this->hasMany('App\Job');
}

}

Here is the model area

class Area extends Model

{ protected $fillable = ['town_id', 'name', 'description'];

public function town(): BelongsTo
{
    return $this->belongsTo(Town::class);
}

 public function user(): BelongsTo
{
    return $this->belongsTo(User::class, 'area_id');
}

}

Here is the code that did not work at the controller

 public function search(Request $request) {
            $service = $request->get('service');
            $area = Input::get('area');

            if(!(empty($service)) && !(empty($area))) {
                $results = User::with(['area', 'service'])
                    ->where('area_id', 'like', "%$area%")
                    ->whereHas('service', function ($query) use ($service) {
                                    $query->where('category_id', $service);
                                   })
                    ->paginate(10);

                return view('Search.search', compact('results'));
            }
           elseif (!(empty($service)) && empty($area)) {
                $results = User::with(['area', 'service'])
                                 ->whereHas('service', function ($query) use ($service) {
                                    $query->where('category_id', $service);
                                   })
                                ->paginate(10);

                return view('Search.search', compact('results'));
            }
             elseif(empty($service) && !empty($area)) {
                $results = User::with(['area', 'service'])
                    ->where('area_id', 'like', "%$area%")
                    ->paginate(10); 
                return view('Search.search', compact('results'));
            }


}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire