【发布时间】:2020-01-25 18:58:22
【问题描述】:
好的,不知道能不能这样,但是我需要把2个表的2个Where子句的结果合并成一个变量。
到目前为止,我正在查询一个表:
$allCompanies = Products::where('category_id', $id->id)->groupBy('company_id')->get();
这是另一个:
$companies = Company::where('visible', 0)->get();
但是有没有办法让它们进入同一个查询字符串?像这样我可以得到 ID 与一个表中的 ID 列匹配的位置,以及另一个表中可见为 0 的位置?
我试过了:
$allCompanies = Products::with('company', function($q){
$q->where('visible', 0);
})->where('category_id', $id->id)
->groupBy('company_id')->get();
但出现此错误:
mb_strpos() expects parameter 1 to be string, object given
公司模式:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
public function products()
{
return $this->hasMany('App\Products');
}
}
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
protected $fillable = [
'name',
'description',
'image',
'size',
'price',
'stock',
'company_id',
'category_id'
];
public function category()
{
return $this->belongsTo('App\Categories', 'category_id');
}
public function company()
{
return $this->belongsTo('App\Company', 'company_id');
}
}
【问题讨论】: