Laravel:根据可选条件从variouos表中获取数据
我想基于可选条件编写一个查询,从不同的表中获取数据。我的模式看起来像 MyBox表Laravel:根据可选条件从variouos表中获取数据,laravel,eloquent,Laravel,Eloquent,我想基于可选条件编写一个查询,从不同的表中获取数据。我的模式看起来像 MyBox表 id, type_id --> foreign key to box_type table postal_code po_box created_at updated_at mybox\u访问表 id mybox_id -> foreign key to myboxes table email 箱形表 id type_name 这是我的模型 MyBox.php c
id,
type_id --> foreign key to box_type table
postal_code
po_box
created_at
updated_at
mybox\u访问表
id
mybox_id -> foreign key to myboxes table
email
箱形表
id
type_name
这是我的模型
MyBox.php
class MyBox extends Model {
public function type() {
return this->hasOne(BoxType::class, 'id', 'type_id');
}
public function access() id
return this->hasOne(MyBoxAccess::class, 'mybox_id', 'type_id');
}
}
php具有以下关系
public function mybox() {
return this->hasOne(MyBox::class, 'id', 'type_id');
}
而MyBoxAccess.php有以下关系
public function vbox() {
return $this->belongsTo(MyBox::class, 'id', 'mybox_id');
}
现在我想得到基于以下条件
我有电子邮件作为所需参数和邮政编码和邮政信箱作为可选参数(但其中一个将是必须的,两者也可以存在)
因此,我想获取所有mybox的数据,这些mybox的id类型为3,或者所有mybox的id与mybox访问表中的电子邮件匹配,并且邮政编码或邮箱与mybox表中的参数匹配
对于参数邮政编码和邮政信箱的简单匹配,我可以写以下内容
$result = new MyBox();
if(!empty($request['postal_code'])) {
$result->where('postal_code', like, '%'.$request['postal_code']);
}
if(!empty($request['po_box'])) {
$result->where('po_box', like, '%'.$request['po_box']);
}
$result = $result->get();
但我不知道如何获取上述条件的数据。当我尝试使用with()时
MyBox::with(['access' => function(Builder $query) use ($request){
$query->where('mybox_id',$request['id']);
}])->get();
我明白了
`Argument 1 Passed to {closure} () must be an instance of Illuminat\Database\Query\Builder, instance of Illuminate\Databaase\Eloquent\Relation\HasOne given`
请任何人告诉我如何根据上述条件获取数据尝试以下查询:
MyBox::with('access')->get();
$query
是一种关系,而不是生成器实例
所以这不应该引发任何异常
MyBox::with(['access' => function ($query) {
$query->where('mybox_id', $request['id']);
}])->get();
但我不认为这能解决你的问题,因为你的盒子访问关系不正确。应该有很多
// MyBox.php
public function type()
{
return $this->hasOne(BoxType::class, 'id', 'type_id');
}
public function access()
{
return $this->hasMany(MyBoxAccess::class, 'mybox_id', 'id');
}
然后在你的控制器中你可以这样做
// $results where type_id is 3
$results = MyBox::where('type_id', 3)->get();
// List of boxes accessible by email
$results = MyBox::whereHas('access', function ($query) {
$query->where('email', request()->input('email'));
})->get();
// Results where postal_code and po_box matches the request
$results = MyBox::with('access')->where(function ($query) {
if (request()->has('postal_code')) {
$query->where('postal_code', 'like', '%' . request()->input('postal_code'));
}
if (request()->has('po_box')) {
$query->where('po_box', 'like', '%' . request()->input('po_box'));
}
})->get();
如果要合并所有条件:
$results = MyBox::where(function ($query) {
if (request()->has('type_id')) {
$query->where('type_id', request()->input('type_id'));
}
if (request()->has('email')) {
$query->whereHas('access', function ($query) {
$query->where('email', request()->input('email'));
});
}
if (request()->has('postal_code')) {
$query->where('postal_code', 'like', '%' . request()->input('postal_code'));
}
if (request()->has('po_box')) {
$query->where('po_box', 'like', '%' . request()->input('po_box'));
}
})->get();
在闭包中使用时,我总是使用request()
facade,这让我感觉更干净。我会使用->get()
三次吗?这取决于具体情况。您询问了3种不同的结果(基于类型id或电子邮件或邮政编码/邮政信箱)。您当然可以只使用1个->get()
@simpondeplchin,我如何将它们组合成一个->get()
,就像每次都会有一个id和emaill类型的必须条件,以及一个post\u code或po\u box?像where(type_id=3或email=myemail)和(post_code=12或po_box=12)?@awaisgarni编辑了我的帖子,添加了一个合并所有条件的示例。您可以根据需要随意更改条件(if/elseif/else)。这样可以解决我在问题中提出的所有问题吗?