Я работаю с красноречивой системой Laravel, и мы получаем неверные числа для наших запросов sql на основе красноречивых моделей.
Запрос к базе данных говорит о 42 записях, но говорит, что общее количество равно 13.
Здесь вы можете видеть, что мы делаем:
$listings = $listings->paginate(100);
Но результаты показывают только 13 записей
Я выполнил это в конце:
file_put_contents("dumpquery.sql","202:".$listings->toSql());
file_put_contents("dump.sql","202:".print_r($listings->getBindings(),true));
и все выглядит правильно.
$listings = Listing::Select('listings.*')->leftJoin('listing_unlikes', function($join) use($user_id) {
$join->on('listing_unlikes.listing_id', '=', 'listings.id')
->where('listing_unlikes.user_id', '=', $user_id);
})->leftJoin('liked_listings', function($join) use($user_id) {
$join->on('liked_listings.listing_id', '=', 'listings.id')
->where('liked_listings.user_id', '=', $user_id);
});
$listings = $listings->whereNull('listing_unlikes.listing_id');
$listings = $listings->whereNull('liked_listings.listing_id');
$listings = $listings->whereNotNull('bio')->where('bio','<>','');
if($request->has('number_of_bedrooms')) {
$request_bedrooms = intval($request->input('number_of_bedrooms'));
$listings->where('number_of_bedrooms', '>=', $request_bedrooms);
}
if($request->has('number_of_bathrooms')) {
$requested_bathrooms = intval($request->input('number_of_bathrooms'));
$listings->where('number_of_bathrooms', '>=', $requested_bathrooms);
}
if($request->has('price_start') && $request->has('price_end')) {
$listings->where('price', '>=', $request->input('price_start'))
->where('price', '<=', $request->input('price_end'));
}
else {
$listings->where('price', '>=', 5000);
}
//Sizing
if($request->has('square_feet')) {
$listings->where('square_feet', '>=', $request->input('square_feet'));
}
//Features
if($request->has('type')) {
$type = '%' . $request->input('type') . '%';
$listings->where('type', 'LIKE', $type);
}
if($request->has('finish')) {
$finish = '%' . $request->input('finish') . '%';
$listings->where('finish', 'LIKE', $finish);
}
if($request->has('property_condition')) {
$condition = '%' . $request->input('property_condition') . '%';
$listings->where('property_condition', 'LIKE', $condition);
}
//Location Filters
if($request->has('zip_code')) {
$listings->where('zip_code', '=', $request->input('zip_code'));
}
if($request->has('town')) {
$listings->where('town', '=', $request->input('town'));
}
if($request->has('county')) {
$listings->where('county', '=', $request->input('county'));
}
if($request->has('city')) {
$city = '%' . $request->input('city') . '%';
$listings->where('city', 'LIKE', $city);
}
if($request->has('address')) {
$address = '%' . $request->input('address') . '%';
$listings->where('address', 'LIKE', $address);
}
$listings=$listings->whereNotNull('address');
$listings=$listings->whereNotNull('zip_code');
$listings=$listings->whereNotNull('city');
$listings=$listings->whereNotNull('county');
$listings=$listings->whereNotNull('town');
// extra tables to include
$listings->with('Agent')->has('Photos')->with('Photos')->with('ListingLikes');
file_put_contents("dumpquery.sql","202:".$listings->toSql());
file_put_contents("dump.sql","202:".print_r($listings->getBindings(),true));
//$listings->with('Agent')->with('Photos')->with('ListingLikes');
$listings = $listings->paginate(100);
return $listings;
Вот вершина дампа var
object(Illuminate\Pagination\LengthAwarePaginator)#420 (9) {
["total":protected]=>
int(13)
["lastPage":protected]=>
int(1)
["items":protected]=>
object(Illuminate\Database\Eloquent\Collection)#707 (1) {
["items":protected]=>
array(13) {
Вот запрос:
select `listings`.* from `listings` left join `listing_unlikes` on
`listing_unlikes`.`listing_id` = `listings`.`id` and
`listing_unlikes`.`user_id` = 14 left join `liked_listings` on
`liked_listings`.`listing_id` = `listings`.`id` and `liked_listings`.`user_id`
= 14 where `listing_unlikes`.`listing_id` is null and
`liked_listings`.`listing_id` is null and `bio` is not null and `bio` <> ""
and `price` >= 5000 and `city` LIKE "%West Palm Beach%" and `address` is not
null and `zip_code` is not null and `city` is not null and `county` is not
null and `town` is not null and exists (select * from `listing_images` where
`listing_images`.`listing_id` = `listings`.`id`)