I need to do paging with two different collection. Have priority listing. Money that is placed in the priority list.
Why do not you do a single query, you can say the priority list. Reason, when using filtering; city, county, category I choose. However, the site owner, having priority listing in the city, county, or even in one of the priority areas listed in the category you want.
So in a query keywords when using 3 criteria, other criteria will be enough in the first. Each page lists 20 records, if the primary listing of the 30 first listing, the second collection of records in the other I would like to be listed thereafter.
According to the criteria normally be with you now I share my code. Waiting for your suggestions. Thank you in advance.
I'm sorry for my bad english.
DB::table('UserAds')
->join('Ads', 'Ads.id', '=', 'UserAds.ad_id')
->join('Users', 'Users.id', '=', 'UserAds.user_id')
->join('AdCategory', 'AdCategory.ad_id', '=', 'Ads.id')
->join('AdEducationPrices', 'AdEducationPrices.ad_id', '=', 'Ads.id')
->join('City', 'City.id', '=', 'Ads.city_id')
->join('Town', 'Town.id', '=', 'Ads.town_id')
->leftJoin('AdMedias', 'AdMedias.ad_id', '=', 'Ads.id')
->select('Ads.*', 'AdEducationPrices.*', 'City.name as city_name', 'City.id as city_id',
'Town.name as town_name', 'Town.id as town_id', 'AdMedias.url')
->where('Ads.is_publish', 1)
->whereIn('AdCategory.category_id', $new_category_array)
->where('Ads.city_id', $city_id)
->where('Ads.town_id', $town_id)
->where('UserAds.is_purchased', true)
->where('UserAds.started_at', '<', date('Y-m-d H:i:s'))
->where('UserAds.finished_at', '>=', date('Y-m-d H:i:s'))
->where('AdMedias.type', 'picture')
->groupby('UserAds.ad_id')->orderby('finished_at', 'DESC')
->orderby('started_at', 'DESC')->paginate(20);
I used array_merge for 2 queries. And filtered records for paginator. Ty for reading my question. I hope to help someone
$new_collection = array_merge($Priority, $Ads);
$new_collection_count = count($new_collection);
$perPage = 20;
$currentPage = Input::get('page', 1) - 1;
$new_collection = array_slice($new_collection, $currentPage * $perPage, $perPage);
$Ads = Paginator::make($new_collection, $new_collection_count, $perPage);
Related
Im new to this Framework, i dont know how to optimize it using db::raw count and aliases and display it to my blade.php using #foreach
im trying to optimize my code, my goals is to count pallet_conditions and store it to my aliases, i dont want to count it one by one like what i did on this code
this is my code not optimize:
//computing the total rapairable
$repairable_total = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where('pallet_condition', '=', 1)
->count();
//REPAIRABLE
//computing the total good pallets
$good_total = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where('pallet_condition', '=', 0)
->count();
//GOOD
this is the code, what i wanted to learn. just to minimize, and use aliases
$result = DB::table('liip_psrm_items')
->select(DB::raw('COUNT(liip_psrm_items.pallet_condition = 0 ) AS condition_1',
'COUNT(liip_psrm_items.pallet_condition = 1 ) AS condition_2'))
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->get();
You can't use single query for two different results, which has totally opposite conditions.
Case 1. You are trying to count the items where pallet_condition = 1;
Case 2. You are trying to count the items where pallet_condition = 0;
Now you want to merge these two cases into single query, which is impossible...
So, For these two cases, you have to use either separate queries ( what you did already )
or you can use single query to grab all the items and then use PHP to separate them.
Like:
$total_items = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->get();
$repairable_count = count(array_filter($total_items, function($item){
return (bool)$item->pallet_condition;
}));
$good_count = count(array_filter($total_items, function($item){
return !(bool)$item->pallet_condition; //just inverse of the above condition
}));
i hope this might help.
To count at multiple condition I used this approach
$lastMonthInvoices = Invoice::select(DB::raw("(COUNT(*)) as count"), DB::raw('SUM(total) as total'),'status')
->whereDate('created_at', '>', Carbon::now()->subMonth())
->groupBy('status')
->get();
i got the result with groupBy Status and in each group total number of records as count & also their sum as total
these two snaps are of one query result
You can, first group by, then get count
Like :
DB::table('liip_psrm_items')
->groupBy('pallet_condition')
->select('pallet_condition', DB::raw('count(*) as total'))
->get();
Try to pass a closure like so:
$results = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where(function($query){
$query->where('pallet_condition', 1)
->orWhere('pallet_condition', 0);
})->count();
I'm using laravel-page-view-counter to count visits of my products and it's working just fine, what i need to do is to get list of top 10 products by their visits (get 10 products which has largest number of visits).
Here is what I have:
$visits = Product::join('page-visits', function ($join) {
$join->on('products.id', '=', 'page-visits.visitable_id');
})->get();
$sorted = $visits->sortBy(function ($product, $key) {
return count($product['visits']);
});
But It return from lowest product visits to highest one (it shows product with 0 visit till product with 100 visits) I need reverse method of that to show (product with 100 visits first).
You can do it easily with query builder and some raw queries like this:
$visits = DB::table('products')
->join('page-visits','products.id','=','page-visits.visitable_id')
->select(DB::raw('count(visitable_id) as count'),'products.*')
->groupBy('id')
->orderBy('count','desc')
->take(10)
->get();
I hope you will understand.
I have articles with comments. I would like to post 5 popular items on my home page based on reviews received over a 7 day period. Is it possible to do this with Laravel? I do not know how to deal with the query builder at all.
I have an article_id in each comment.
$mostPopular = Article::published()->whereHas('comments', function ($query){
$query->count();
})->orderBy($query, 'ASC');
thank you !
Untested, but should satisfy your conditions. Let me know if you have any issues.
$popularArticles = Article::published()
->whereHas('comments')
->withCount('comments')
->where('created_at', '>', \Carbon\Carbon::now()->subWeek())
->orderBy('comments_count', 'DESC')
->take(5)
->get();
I have articles with comments. I would like to post 5 popular items on my home page based on reviews received over a 7 day period. I tried this :
$popularArticles = Article::published()
->whereHas('comments')
->withCount('comments')
->where('created_at', '>', \Carbon\Carbon::now()->subWeek())
->orderBy('comments_count', 'DESC')
->take(5)
->get();
But with this method, articles that were created more than 7 days ago are "ignored". What I would like is that it is the comments of the last 7 days that define whether an article is popular or not.
Thank you very much
You need to filter counted comments like this:
$popularArticles = Article::published()
->has('comments')
->withCount(['comments' => function ($q) {
$q->where('created_at', '>', Carbon\Carbon::now()->subWeek());
}])
->latest('comments_count')
->take(5)
->get();
I wondered how to make a Where All clause with Laravel
I'm trying to check if the episodes that a user saw are all the episodes of the series.
I'm using the WhereIn clause but i returns the results if i saw one episode of the serie.
$alleps get all the episodes of the serie
$seriessaw get all the episodes a user saw
Thank you for your answers !
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id);
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
I think you would need to set a having clause which would force records to only return if there's the same amount of records as there are amount of episodes.
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->having(\DB::raw('count(*)'), count($alleps))
->get();
You should note however that this would likely break if there's any possibility of having duplicates in the userepisodes table.
When you use WhereIn, you have to pass an array as the second parameter.
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id)->get()->toArray();
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();