如何使用Laravel按类别和区域进行搜索 [英] How to do a Search By Category and Area using Laravel

查看:67
本文介绍了如何使用Laravel按类别和区域进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过用户所属的类别以及用户所在的区域来搜索用户.

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

我有4张桌子,其中包括

I have 4 tables for that which include,

包含ID,名称,姓氏,电子邮件,电话,类型,照片和area_id的用户表.

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

具有ID,名称,描述的类别表

A category table with id, name, description

具有ID,名称,描述和category_id的服务表.

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

我在Service和User之间有一个数据透视表,名为service_user,具有属性ID,名称,价格,描述,user_id和service_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.

我希望知道如何使用Laravel在数据库中存在所有信息的情况下按用户的类别和区域搜索用户.我已经设法仅按区域进行搜索,并且可以正常工作.刚才剩下的是我不知道为什么它不起作用或不知道要做什么的类别,因为在我的数据库中,类别ID迁移到服务表,而且service_id与user_id一起迁移到数据透视表(service_user).因此,这是用户与服务之间的多对多关系.

I wish to know how can I search users 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('services.category.category_id', 'like', "%$category%")
            ->where('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('services.category.category_id', 'like', "%$category%")
            ->get();
        //dd($results);

        return view('Search.search', compact('results'));
    }
     elseif(empty($category) && !empty($area)) {
        $results = User::with(['area', 'services'])
            ->where('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= "{{ route('search') }}" 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="{{ $category->id }}">{{ $category->name }}</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="{{ $area->id }}">{{ $area->name }}</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>

有人可以帮我解决这个问题吗?我将很感激.

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

推荐答案

类似的方法可能对您有用

Something like this might work for you

User::with(['area', 'services'])
    ->where('type', 'jobbers')
    ->where('area_id', $area_id)
    ->whereHas('services', function ($query) use ($category_id) {
        $query->where('category_id', $category_id);
    })
    ->get();

这篇关于如何使用Laravel按类别和区域进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆