Laravel或者与相关的表

时间:2014-11-23 11:38:04

标签: php mysql laravel laravel-4

我正在尝试从相关表中获取行:

        $Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
                ->orWhere('brand', 'LIKE', '%' . $filters['search'] . '%');

但它不起作用,因为它只从products表中选择。

我的产品型号是:

class Product extends Eloquent {

    protected $table = 'products';

    public function brands() {
        return $this->hasOne('ProductBrands', 'id', 'brand_id');
    }

    public function ages() {
        return $this->hasOne('ProductAges', 'id', 'age_id');
    }

    public function types() {
        return $this->hasOne('ProductTypes', 'id', 'type_id');
    }

    public function images() {
        return $this->hasMany('ProductImages');
    }

    public function toArray() {

        $ar = $this->attributes;

        $ar['brand'] = $this->brand;
        $ar['age'] = $this->age;
        $ar['type'] = $this->type;

        return $ar;
    }

    public function getBrandAttribute() {
        $brands = $this->brands()->first();
        return (isset($brands->brand) ? $brands->brand : '');
    }

    public function getAgeAttribute() {
        $ages = $this->ages()->first();
        return (isset($ages->age) ? $ages->age : '');
    }

    public function getTypeAttribute() {
        $types = $this->types()->first();
        return (isset($types->type) ? $types->type : '');
    }

}

如果正确指定关系,我不确定如何继续。

如何从主表和/或相关表products_brands中选择?


在orWheres中添加后,我的过滤器的其余部分已损坏:

public function fetchProducts($filters, $sorts = null, $perpage = 2) {
    $Product = Product::query();
    if (!empty($filters['search']))
        $Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
                ->orWhereHas('brands', function($q) use ($filters) {
                    $q->where('brand', 'LIKE', '%' . $filters['search'] . '%');
                })
                ->orWhereHas('types', function($q) use ($filters) {
                    $q->where('type', 'LIKE', '%' . $filters['search'] . '%');
                });
    if (isset($filters['type']))
        $Product->where('type_id', $filters['type']);
    if (isset($filters['brand']))
        $Product->where('brand_id', $filters['brand']);
    if (isset($filters['age']))
        $Product->where('age_id', $filters['age']);

    if (isset($sorts['sort']))
        $Product->orderBy($sorts['sort'], $sorts['sortdir']);

    return $Product;
}

如果search和其他一个或多个都存在,例如age,它只遵循(!empty($filters['search']))

中的内容

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

要为关系编写where条件,您可以使用whereHas。 (或orWhereHas在你的情况下我)Laravel Docs

试试这个:

$Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
        ->orWhereHas('brands', function($q) use ($filters){
            $q->where('brand', 'LIKE', '%' . $filters['search'] . '%');
        });

更新

为了保存这一切,你最好使用嵌套的地方

$Product = Product::query();
if (!empty($filters['search'])){
    $Product->where(function($q) use ($filters){
        $q->where('name', 'LIKE', '%' . $filters['search'] . '%')
        $q->orWhereHas('brands', function($q) use ($filters) {
            $q->where('brand', 'LIKE', '%' . $filters['search'] . '%');
        });
        $q->orWhereHas('types', function($q) use ($filters) {
            $q->where('type', 'LIKE', '%' . $filters['search'] . '%');
        });
}
if (isset($filters['type']))
    $Product->where('type_id', $filters['type']);
if (isset($filters['brand']))
    $Product->where('brand_id', $filters['brand']);
if (isset($filters['age']))
    $Product->where('age_id', $filters['age']);

if (isset($sorts['sort']))
    $Product->orderBy($sorts['sort'], $sorts['sortdir']);

return $Product;