Laravel 通过(可变数量的)多个键进行Larvel搜索

Laravel 通过(可变数量的)多个键进行Larvel搜索,laravel,orm,eloquent,Laravel,Orm,Eloquent,我有一个中等复杂的搜索表单: 我需要能够接受这些字段的任意组合,并返回相应的发布对象列表。除了退出雄辩和有条件地编写SQL之外,我应该如何有条件地构建选择 与我的早期版本相比,表布局得到了简化: Posting->profile->library_type_id Posting->profile->org_name Posting->profile->province_id 在过去的一周里,我收到了一些不可行的建议。目前,我有一个SearchServic

我有一个中等复杂的搜索表单:

我需要能够接受这些字段的任意组合,并返回相应的发布对象列表。除了退出雄辩和有条件地编写SQL之外,我应该如何有条件地构建选择

与我的早期版本相比,表布局得到了简化:

Posting->profile->library_type_id
Posting->profile->org_name
Posting->profile->province_id 
在过去的一周里,我收到了一些不可行的建议。目前,我有一个SearchServiceProvider,它由以下几行组成:

<?php namespace BCLA\Search;

use Posting;
use Profile;
use Input;
use DB;

class Search {

public function posts()
{
    $type_id  = Input::get('type_id');
    $org_name = Input::get('org_name');
    $province_id = Input::get('province_id');
//    $city     = Input::get('city');
//    $keywords = Input::get('keywords');

    $profiles = DB::table('profiles')
                ->select(DB::raw('id'))
                ->where('library_type_id','=',$type_id)
                ->where('org_name','LIKE',"%$org_name%")
                ->where('province_id','=',$province_id)
                ->get();

    dd($profiles);

    $posts;
    return $posts;
}
} 
在dd$profiles上;我始终得到一个空对象,即使所有where子句解析为有效值。我在数据库中只有少数记录,因此我确信我的搜索输入是有效的

如果您有任何关于如何继续的想法,我将不胜感激。 -Erik

您可以在->get之后添加->toSql->以查看查询,可能存在一些问题。Input::get。。如果您碰巧没有设置任何请求变量,则返回null,因此很明显,除非数据库中有null值,否则query不会返回任何行,我认为情况并非如此@delmadord最好使用DB::getQueryLog,因为它显示所有查询。或者你可以使用。但是->toSql在get上不起作用,因为后者返回一个集合。
<?php namespace BCLA\Search;

use Posting;
use Profile;
use Input;
use DB;

class Search {

public function posts()
{
    $type_id  = Input::get('type_id');
    $org_name = Input::get('org_name');
    $province_id = Input::get('province_id');
//    $city     = Input::get('city');
//    $keywords = Input::get('keywords');

    $profiles = DB::table('profiles')
                ->select(DB::raw('id'))
                ->where('library_type_id','=',$type_id)
                ->where('org_name','LIKE',"%$org_name%")
                ->where('province_id','=',$province_id)
                ->get();

    dd($profiles);

    $posts;
    return $posts;
}
}