我如何使用 Eloquent 过滤表并同时连接表?
How i can filter, with Eloquent, either in tables and join tables at the same time?
我有两个 table,product_template
和 shorttitles
。
一个产品可以有多个短标题,具体取决于语言
产品有主标题(称为 mmac_ebay_titolo
),短标题有名称,即不同语言的标题。
我的目标 是根据 post 输入过滤短片标题和 product_template带有字符串 posted.
的标题
如果没有传递过滤字符串,一切正常。
shorttitles
模型有这样的关系:
public function prodotto()
{
return $this->belongsTo('App\Product', 'product_id', 'id');
}
product_template
模型有这样的关系:
public function shorttitle()
{
return $this->hasMany('App\ShortTitle', 'product_id', 'id');
}
但我坚持使用下面的代码:
$m = self::MODEL;
$query = $m::select("*");
$data["data"] = $query
->with(["prodotto" => function ($q) use ($params) {
$q->select("id", "name", "mmac_ebay_titolo")
->where("name", "like", "%" . $params["search"] . "%");
}]
)->where("lang_id", "=", 1)
->offset($start)
->limit($limit)
->orderBy("id", "asc")
->get();
此查询 returns 所有短片...如果子查询与 where 子句不匹配,则 "prodotto" = null。为什么?
在此之后,我将过滤主要的短标题table:
$m = self::MODEL;
$query = $m::select("*");
$data["data"] = $query
->with(["prodotto" => function ($q) use ($params) {
$q->select("id", "name", "mmac_ebay_titolo")
->where("mmac_ebay_titolo", "like", "%" . $params["search"] . "%");
}]
)->where("lang_id", "=", 1)
->where("name", "like", "%" . $params["search"] . "%")
->offset($start)
->limit($limit)
->orderBy("id", "asc")
->get();
我想重现这个查询:
Select
s.name,
p.name,
p.mmac_ebay_titolo
From
mmac_brx_ebay_shorttitles s Inner Join
product_template p On s.product_id = p.id
where lang_id = 1
and
(
s.name like '%PIGNONE%'
or
p.name like '%PIGNONE%'
or
p.mmac_ebay_titolo like '%PIGNONE%'
)
我怎样才能做到这一点?
谢谢!
试试这个查询:
$products = $query->join('product_template','mmac_brx_ebay_shorttitles.product_id','=','product_template.id')
->where(function ($query) {
$query->Where('mmac_brx_ebay_shorttitles.name','like','%PIGNONE%')
->orWhere('product_template.name','like','%PIGNONE%')
->orWhere('product_template.mmac_ebay_titolo','like','%PIGNONE%');
})->where('your_table_name.lang_id',1)
->select('mmac_brx_ebay_shortitles.name','product_template.name','product_template.mmac_ebay_titolo')
->paginate(10);
我有两个 table,product_template
和 shorttitles
。
一个产品可以有多个短标题,具体取决于语言
产品有主标题(称为 mmac_ebay_titolo
),短标题有名称,即不同语言的标题。
我的目标 是根据 post 输入过滤短片标题和 product_template带有字符串 posted.
的标题如果没有传递过滤字符串,一切正常。
shorttitles
模型有这样的关系:
public function prodotto()
{
return $this->belongsTo('App\Product', 'product_id', 'id');
}
product_template
模型有这样的关系:
public function shorttitle()
{
return $this->hasMany('App\ShortTitle', 'product_id', 'id');
}
但我坚持使用下面的代码:
$m = self::MODEL;
$query = $m::select("*");
$data["data"] = $query
->with(["prodotto" => function ($q) use ($params) {
$q->select("id", "name", "mmac_ebay_titolo")
->where("name", "like", "%" . $params["search"] . "%");
}]
)->where("lang_id", "=", 1)
->offset($start)
->limit($limit)
->orderBy("id", "asc")
->get();
此查询 returns 所有短片...如果子查询与 where 子句不匹配,则 "prodotto" = null。为什么?
在此之后,我将过滤主要的短标题table:
$m = self::MODEL;
$query = $m::select("*");
$data["data"] = $query
->with(["prodotto" => function ($q) use ($params) {
$q->select("id", "name", "mmac_ebay_titolo")
->where("mmac_ebay_titolo", "like", "%" . $params["search"] . "%");
}]
)->where("lang_id", "=", 1)
->where("name", "like", "%" . $params["search"] . "%")
->offset($start)
->limit($limit)
->orderBy("id", "asc")
->get();
我想重现这个查询:
Select
s.name,
p.name,
p.mmac_ebay_titolo
From
mmac_brx_ebay_shorttitles s Inner Join
product_template p On s.product_id = p.id
where lang_id = 1
and
(
s.name like '%PIGNONE%'
or
p.name like '%PIGNONE%'
or
p.mmac_ebay_titolo like '%PIGNONE%'
)
我怎样才能做到这一点?
谢谢!
试试这个查询:
$products = $query->join('product_template','mmac_brx_ebay_shorttitles.product_id','=','product_template.id')
->where(function ($query) {
$query->Where('mmac_brx_ebay_shorttitles.name','like','%PIGNONE%')
->orWhere('product_template.name','like','%PIGNONE%')
->orWhere('product_template.mmac_ebay_titolo','like','%PIGNONE%');
})->where('your_table_name.lang_id',1)
->select('mmac_brx_ebay_shortitles.name','product_template.name','product_template.mmac_ebay_titolo')
->paginate(10);