Php Laravel或WHERE与相关表格
我正在尝试从相关表中获取行,其中包含:Php Laravel或WHERE与相关表格,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,我正在尝试从相关表中获取行,其中包含: $Product->where('name', 'LIKE', '%' . $filters['search'] . '%') ->orWhere('brand', 'LIKE', '%' . $filters['search'] . '%'); 但它不起作用,因为它只从产品表中进行选择 我的产品型号是: class Product extends Eloquent { protect
$Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
->orWhere('brand', 'LIKE', '%' . $filters['search'] . '%');
但它不起作用,因为它只从产品
表中进行选择
我的产品型号是:
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 : '');
}
}
正确指定关系后,我不确定如何继续
如何从主表和/或相关表中选择产品和品牌
在添加Orwhere后,我的过滤器的其余部分已损坏:
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;
}
如果同时存在搜索
和一个或多个其他项,例如年龄
,则它只遵守If(!empty($filters['search'])中的内容
如何解决此问题?要为关系编写where条件,可以使用
whereHas
。(或或您案例中的where has
)
试试这个:
$Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
->orWhereHas('brands', function($q) use ($filters){
$q->where('brand', 'LIKE', '%' . $filters['search'] . '%');
});
更新
为了保存所有的where,最好使用嵌套where
$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;
要为关系编写where条件,可以使用
whereHas
。(或或您案例中的where has
)
试试这个:
$Product->where('name', 'LIKE', '%' . $filters['search'] . '%')
->orWhereHas('brands', function($q) use ($filters){
$q->where('brand', 'LIKE', '%' . $filters['search'] . '%');
});
更新
为了保存所有的where,最好使用嵌套where
$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;
我只是注意到这破坏了一些东西,请查看我的编辑:)不确定这是否是问题所在,但是如果内容不只是一行,则必须使用if和
{}
。它是否将其他if用作or?因为这是错误的,那些其他的if需要是and,而那些or wherehas需要在原始SQL语句的括号中。不客气。可悲的是,我没有得到任何评论投票的代表。。。但是,嘿,我并不是为了名誉才这么做的;)我只是注意到这破坏了一些东西,请查看我的编辑:)不确定这是否是问题所在,但是如果内容不只是一行,则必须使用if和{}
。它是否将其他if用作or?因为这是错误的,那些其他的if需要是and,而那些or wherehas需要在原始SQL语句的括号中。不客气。可悲的是,我没有得到任何评论投票的代表。。。但是,嘿,我并不是为了名誉才这么做的;)