Laravel使用hasMany进行搜索

时间:2014-07-11 19:28:18

标签: php search laravel eloquent

我有一个可以有很多产品的工作模型。

Job.php
class Job extends Eloquent
{
    ....
    public function products()
    {
        return $this->hasMany('Products');
    }
    ....
}

现在我希望用户能够按产品搜索作业。 搜索产品的表单:

job.search.blade.php
....
<div class="form-group required has-feedback products displayed">
    {{ Form::label('product_id', 'Product', array('class' => 'control-label')) }}
    {{ Form::select('product_id', Product::product_select(), Input::old('product_id'), array('class' => 'form-control', 'required' => 'required')) }}
    <span id="add_product_button" class="glyphicon glyphicon-plus-sign form-control-feedback"></span>
</div>
<div class="form-group products hidden">
    {{ Form::label('product_id_2', 'Product #2', array('class' => 'control-label')) }}
    {{ Form::select('product_id_2', Product::product_select(), Input::old('product_id_2'), array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group products hidden">
    {{ Form::label('product_id_3', 'Product #3', array('class' => 'control-label')) }}
    {{ Form::select('product_id_3', Product::product_select(), Input::old('product_id_3'), array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group products hidden">
    {{ Form::label('product_id_4', 'Product #4', array('class' => 'control-label')) }}
    {{ Form::select('product_id_4', Product::product_select(), Input::old('product_id_4'), array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group products hidden">
    {{ Form::label('product_id_5', 'Product #5', array('class' => 'control-label')) }}
    {{ Form::select('product_id_5', Product::product_select(), Input::old('product_id_5'), array('class' => 'form-control', 'required' => 'required')) }}
</div>
....

在JobController中,使用实例化变量:

$query = Job::orderBy('job_id', 'DESC');

如何获得用户声明的产品?

我的尝试越来越近,但我已经被困了一段时间了。

JobController.php
....
if (Input::has('product_id') && Input::get('product_id') != 0)
{
    $query->with(array('products' => function($products)
    {
        $products->where('product_id', '=', Input::get('product_id'));
    }));
}
$jobs = $query->paginate(15);
....

修改:已解决

我显然无法使用'with'进行搜索,所以我不得不使用SQL连接。 http://forumsarchive.laravel.io/viewtopic.php?id=13238

因此,使用hasMany()进行搜索,

if (Input::has('product_id') && Input::get('product_id') != 0)
{
    $query->where('inventory.product_id', '=', Input::get('product_id'));
}

if (Input::has('product_id_2') && Input::get('product_id_2') != 0)
{
    $query->leftJoin('inventory AS inventory_2', 'inventory_2.job_id', '=', 'jobs.id');
    $query->where('inventory_2.product_id', '=', Input::get('product_id_2'));
}

if (Input::has('product_id_3') && Input::get('product_id_3') != 0)
{
    $query->leftJoin('inventory AS inventory_3', 'inventory_3.job_id', '=', 'jobs.id');
    $query->where('inventory_3.product_id', '=', Input::get('product_id_3'));
}

if (Input::has('product_id_4') && Input::get('product_id_4') != 0)
{
    $query->leftJoin('inventory AS inventory_4', 'inventory_4.job_id', '=', 'jobs.id');
    $query->where('inventory_4.product_id', '=', Input::get('product_id_4'));
}

if (Input::has('product_id_5') && Input::get('product_id_5') != 0)
{
    $query->leftJoin('inventory AS inventory_5', 'inventory_5.job_id', '=', 'jobs.id');
    $query->where('inventory_5.product_id', '=', Input::get('product_id_5'));
}

如果有人可以推荐我这样做的有效方法,请告诉我。

谢谢。

2 个答案:

答案 0 :(得分:2)

$products; // array of ids from the form

$jobs = Job::whereHas('products', function ($q) use ($products) {
   $q->whereIn('products.id', $products);
}, '=', count($products))->get();

答案 1 :(得分:1)

使用whereIn方法:

$query->with(array('products' => function($query)
{
    $productIds = Input::only('product_id', 'product_id_2', 'product_id_3');

    $query->whereIn('product_id', $productIds);
}));

您应该使用一系列产品ID而不是编号,但那是另一天的讨论。