lundi 23 avril 2018

How to do a Search By Category and Area using Laravel

I want to be able to search user with type = jobbers by the category to which they belong and also by the area in which they are located.

I have 4 tables for that which include,

A User table with id, name, surname, email, phone, type, photo and area_id.

A category table with id, name, description

A Service table with id, name, description, and category_id.

and I have a Pivot table between Service and User named service_user with attributes id, name, price, description, user_id, and service_id.

I wish to know how can I search users with type = jobber by their category and area, provided all the information exists in the database using Laravel. I have managed to do a search by area alone and it works. Just now remaining the category which I don't know why it's not working or what exactly to do since in my database the category id migrates to the service table, also the service_id migrates to the pivot table(service_user) alongside with the user_id. So it's a many to many relationship btw users and services.

In my web file, I have this

Route::get('/search', 'UserController@search')->name('search');

In my user Controller, I did something like this

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

    //dd($category);
    //dd($area);

    // $p = $town->id;
    // $v = Input::get('town.$town->id');
    //$va = $request->getPathInfo();

    if(!(empty($category)) && !(empty($area))) {
        $results = User::with(['area', 'services', 'category'])
            ->where('users.services.category_id', 'like', "%$category%")
            ->where('users.area_id', 'like', "%$area%")
            ->get();

        //Section::inject('title','Search');
       // dd($results);
        return view('Search.search', compact('results'));
    }
   elseif (!(empty($category)) && empty($area)) {
        $results = User::with(['area', 'services'])
            ->where('users.services.category_id', 'like', "%$category%")
            ->get();
        //dd($results);

        return view('Search.search', compact('results'));
    }
     elseif(empty($category) && !empty($area)) {
        $results = User::with(['area', 'services'])
            ->where('users.area_id', 'like', "%$area%")
            ->get();
        //dd($results);
        return view('Search.search', compact('results'));
    }

    return view('Users/jobberlist');
}

and in my Home page with the search form, I did this

<form action= "" method="GET">
    <div class="row">
        <div class="col-lg-7 col-md-5 col-sm-5 col-xs-12">
            <div class="job-field">
                <select name="category">
                    <option value="">Je recherche une personne pour...</option>

                    @foreach ($categories as $category)

                    <option value=""></option>

                     @endforeach
                </select>
            </div>
        </div>
        <div class="col-lg-4 col-md-5 col-sm-5 col-xs-12">
            <div class="job-field">
                <select data-placeholder="City, province or region" class="chosen-city" name="area">
                     <option value="">Choissisez un quartier...</option>
                    @foreach ($areas as $area)

                    <option value=""></option>

                     @endforeach
                </select>
                <i class="la la-map-marker"></i>
            </div>
        </div>
        <div class="col-lg-1 col-md-2 col-sm-2 col-xs-12">
            <button type="submit"><i class="la la-search"></i></button>
        </div>
    </div>
</form>

and to get the values of the search result in the search.blade.php I did this

<div class="job-list-modern">
    <div class="job-listings-sec no-border">
        @foreach ($results as $result)
        <div class="job-listing wtabs">
            <div class="job-title-sec">
                <div class="c-logo"> <img src="" alt="" /> </div>
                <h3><a href="" title="">  <span style="float:right; padding-left: 10px;"> (<i class="la la-eye" style="font-size: 18px;"></i> Vu  fois)</span></a></h3>
                <span></span>
                <div class="job-lctn"><i class="la la-map-marker"></i>, </div>
            </div>
            <div class="job-style-bx"><a href="" class="job-is ft">Voir Profil</a>
<span class="fav-job"><i class="la la-heart-o"></i></span>
                <i>5 months ago</i>
            </div>
        </div>
        @endforeach
    </div>

and in my user model I did this

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

 public function services(): BelongsToMany
{
    return $this->belongsToMany(Service::class)->withPivot('title', 'Description', 'price');
}

public function getCategoryAttribute(): Category
{
    return $this->services->category;
}

and In my service Model I did this

 public function users(): BelongsToMany
{
    return $this->belongsToMany(User::class);
}


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

Can someone help me out to solve this problem? I will gladly appreciate.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Fa33hv
via IFTTT

Aucun commentaire:

Enregistrer un commentaire