Laravel或者与相关的表

imp*_*335 0 php mysql laravel laravel-4

我试图从相关的表中获取行:

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

但它不起作用,因为它只是从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 : '');
    }

}
Run Code Online (Sandbox Code Playgroud)

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

如何从主表和/或相关表中进行选择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;
}
Run Code Online (Sandbox Code Playgroud)

如果两者search和一个或多个其他人存在,例如age,它只服从if中的什么(!empty($filters['search']))

我怎样才能解决这个问题?

luk*_*ter 6

写一个你可以使用的关系的where条件whereHas.(或orWhereHas在你的情况下我)Laravel Docs

试试这个:

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

更新

为了保存所有这些,你最好使用嵌套的地方

$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;
Run Code Online (Sandbox Code Playgroud)