我正在我们的 User 表上运行各种 Eloquent 查询,以根据各种条件查找当前登录成员的成员匹配项。
一个查询会根据当前登录的会员最想访问的地点与匹配的会员也最想访问的地点返回匹配的会员。
$match1 = User::with(['country', 'cityLocation']);
$match1 = $match1->where(function ($query) use($memberFavoPlaceArrayCleaned) {
foreach($memberFavoPlaceArrayCleaned as $searchWord){
if ($searchWord != "") {
$query = $query->orWhere('favoPlace', 'LIKE', '%'. $searchWord .'%');
}
}
return $query;
});
$match1 = $match1->where("id", "!=", strval($member->id));
$match1 = $match1->where("userSuspended", "!=", 1);
$match1 = $match1->where("meetup", "!=", 0);
$match1 = $match1->where(function ($query) {
return $query->where('status', '=', 'approved')
->orWhere('status', '=', 'approvedfinal');
});
$match1 = $match1->orderBy('id','desc');
$match1 = $match1->skip(0)->take(20);
$match1 = $match1->get();
另一个返回位于当前登录会员想要访问的位置的会员。
$match2CountryIDs = FbcCoreCountry::where('active', 1);
$match2CountryIDs = $match2CountryIDs->where(function ($query) use($memberFavoPlaceArrayCleaned) {
foreach($memberFavoPlaceArrayCleaned as $searchWord){
if ($searchWord != "") {
$query = $query->orWhere('nicename', '=', $searchWord);
}
}
return $query;
})
->orderBy('nicename')
->get();
// Now return all members in those country IDs
if ($match2CountryIDs->isEmpty()) {
$match2 = null;
} else {
$match2 = User::with(['country', 'cityLocation']);
$match2 = $match2->where(function ($query) use($match2CountryIDs) {
foreach($match2CountryIDs as $country){
if ($country->id != "") {
$query = $query->orWhere('countryId', '=', $country->id);
}
}
return $query;
});
$match2 = $match2->where("id", "!=", strval($member->id));
$match2 = $match2->where("userSuspended", "!=", 1);
$match2 = $match2->where("meetup", "!=", 0);
$match2 = $match2->where(function ($query) {
return $query->where('status', '=', 'approved')
->orWhere('status', '=', 'approvedfinal');
});
$match2 = $match2->orderBy('id','desc');
$match2 = $match2->skip(0)->take(20);
$match2 = $match2->get();
}
我现在想查看是否有任何成员同时出现在 $match1 和 $match2 中,因为这些成员将被视为比仅出现在其中一个结果中的成员更匹配当前成员。
实际上,由于我们有额外的匹配条件,因此实际上有两个以上的查询。实际上,我们有 $match1、$match2、$match3、$match4、$match5,但上面的内容应该足以显示查询是如何构建的。
我需要实现的目标如下:
相互检查所有结果并创建一个新结果,其中包括所有返回的成员,每个成员有一个条目。我还需要为结果中的每个成员添加一个新列 - resultCount - 这是一个数值,显示在 - 0、或 1、或 2、或 3、或 4、或 5 中找到该成员的结果数。
每个成员在最终结果中只能出现一次,包含其所有数据,加上 resultCount 的附加值。
在 Laravel / Eloquent 中实现这一目标的最佳方法是什么?
您似乎没有使用很多 Laravel 查询功能。此外,不要忘记 get();最后查询,因为这会检索模型的集合。如果你只想要一个数组,在 get() 之后链 ->toArray();
代替
$matches1 = $matches1->where("meetup", "!=", 0);
你可以做
$matches1 = $matches1->whereNot("meetup", 0)->get();
但更重要的是: 我看不到你的数据库设计,但是我猜测你没有使用关系数据库。我强烈建议你将数据库设计为关系数据库,因为它是当今的标准面向对象数据库,简单地说,因为 MySQL(我猜这就是您使用的)是一个关系数据库管理系统。 在此处阅读有关关系数据库的更多信息并查看其他网站/视频:
https://www.w3schools.com/mysql/mysql_rdbms.asp如果您已经设置了关系数据库,请设置 Laravel 关系以利用 Laravel 的功能。 请阅读此处了解 Laravel 关系:
https://laravel.com/docs/11.x/eloquent-relationships#main-content如果您设置了关系数据库并正在使用 Laravel 的关系,您可以像这样执行查询:
$users = User::whereHas('city', function ($query) use ($city_id) {
$query->where('id', $city_id);
})
->whereHas('color', function ($query) use ($color_id) {
$query->where('id', $color_id);
})
->whereHas('car', function ($query) use ($car_id) {
$query->where('id', $car_id);
})
->whereHas('model', function ($query) use ($model_id) {
$query->where('id', $model_id);
})
->where('active', 0)
->get();
你就完成了。
您还需要了解链接查询:
https://laravel.com/docs/master/queries $matches = User::whereNot("id", strval($member->id))
->whereNot("userSuspended", 1)
->whereNot("meetup", 0)
->where("city", "New York")
->get();
但是,再次了解关系数据库,这可能是一项繁重的工作,一开始可能会很困难,但它会让你知识更丰富。好消息是,当您编写复杂的程序并且数据库的内存大小要小得多时,它会让生活变得更加轻松。
总结:
了解关系数据库与面向对象数据库