if ($request->building_id == null) {
$posts = Post::with('buildings')
->published()
->latest('published_at')
->whereCategoryId($request->posts_type)
->offset(($request->page - 1) * 6)
->take(6)
->get();
} else {
$posts = Post::published()
->latest('published_at')
->whereCategoryId($request->posts_type)
->whereHas('buildings', function ($query) use ($request) {
$query->whereId($request->building_id);
})
->offset(($request->page - 1) * 6)
->take(6)
->get();
};
$query = Post::whereCategoryId($request->posts_type);
if (!empty($request->building_id)) {
$query = $query->with('buildings');
} else {
$query = $query->whereHas('buildings', function ($query) use ($request) {
$query->whereId($request->building_id);
});
}
$posts = $query->published()
->latest('published_at')
->offset(($request->page - 1) * 6)
->take(6)
->get();
$buildingId = $request->input('building_id');
Post::published()
->whereCategoryId($request->input('posts_type'))
->when($buildingId, function ($query, $buildingId) {
return $query->whereHas('buildings', function ($query) use($buildingId) {
$query->whereId($buildingId);
});
})
->with('buildings')
->latest('published_at')
->paginate(6);
$posts = Post::with('buildings');
$posts->where('1', '=', '2');
$posts->where('3', '=', '4');
$posts->orderBy('id', 'desc');