Please don't tell me to put the orderBy inside the function, because that doesn't work. kill_count and death_count are columns of the roleplay relationship
here is my full code:
$topCredits = Player::whereHas("roleplay", function($q) {
$q->where('government_id', '<', '1');
})->orderBy('credits', 'DESC')->limit(10)->get();
$topKills = Player::whereHas("roleplay", function($q) {
$q->where('government_id', '<', '1')->orderBy('kill_count', 'DESC');
})->limit(10)->get();
$topDeaths = Player::whereHas("roleplay", function($q) {
$q->where('government_id', '<', '1')->orderBy('death_count', 'DESC');
})->limit(10)->get();
Now, the first $topCredits works perfectly, but the bottom 2 don't. They work very well except for the fact it doesn't order by the death_count and kill_count, can anyone help with this? The roleplay is a relationship of hasOne on both sides.
The orderBy in the whereHas would not have any effect on players. Since whereHas is a subquery of Payers.
$topKills = Player::whereHas("roleplay", function($q) {
$q->where('government_id', '<', '1');
})->leftJoin("roleplay","roleplay.player_id","=","players.id")
->select("players.*")
->orderBy('roleplay.kill_count', 'DESC')
->limit(10)
->get();
Hope this helps
Related
So my code is
$categories=Category::where('user_id', $store->id)->whereHas('childrenCategories', function($q) use ($id){
$q->where('user_id', $id);
})->orwhereHas('products', function($q) use ($id) {
$q->where('auth_id', $id);
})->with('products', 'childrenCategories')->latest()->get();
I want to get all children categories with and products with given id but this code doesn't seem to work. As children categories with user_id other than id are also being returned. Sorry, I am relatively new to Laravel. And I thought this would be a good platform to ask. Also, I can share the relationships if you want me to.
I solved this with the following. Thanks, #lagbox for your time.
$categories = Category::with(['childrenCategories' =>
$childrenClosure = function ($query) use ($id) {$query->where('user_id', $id);}, 'products'
=> $productsClosure = function ($query) use ($id) {$query->where('auth_id', $id);}])
->where('user_id', $store->id)
->where(function ($query) use ($childrenClosure, $productsClosure) {$query->whereHas('childrenCategories', $childrenClosure)
->orWhereHas('products', $productsClosure);})
->latest()
->get();
i have a problem when doing "orderBy" name product. here are my codes
$resume = Transaction::with(['product' => function ($q) {
$q->orderBy('name_product','ASC');
}])
->where('status', 'keluar')
->where('status', 'masuk')
->get();
but my code its not working... here is the output
result
Use the collection to sort instead. It allows for sorting based on a nested property.
$resume = Transaction::with('product')
->where('status', 'keluar')
->where('status', 'masuk')
->get()
->sortBy('product.name_product')
->values();
Because you are applying condition to product, not in transaction.
I have another suggestion use whereHas.
Transaction::whereHas('product', function($query) {
$query->->orderBy('name_product','ASC');
})->where('status', 'keluar')
->where('status', 'masuk')
->get();
I have a problem with selecting only rows with relations. I've tried many different solutions. I have 2 tables Movies and Movie_links. I want to get only movies which have links.
Models:
Movie.php
public function links()
{
return $this->hasMany('App\Movie_link', 'movie', 'id');
}
Movie_link.php
public function movie()
{
return $this->belongsTo('App\Movie', 'movie','id')->first();
}
Controller:
I use this code to get all movies which have links and this code works but i want something more efficient:
Movie::latest()
->select('id', 'title', 'poster', 'rating')
->whereIn('id', Movie_link::select('movie')->distinct()->pluck('movie'))
->wherenotnull('poster')
->limit(14)
->get()
But I want more performance so I want to use this code:
Movie::has('links')
->select('id', 'title', 'poster', 'rating')
->withCount('links')
->orderByDesc('id')
->limit(14)
->get()
But with this code request takes 15 seconds! If I change DESC to ASC it is good.
How about using a join:
Movie::query()->from('Movies as M')
->join('Movie_links as L', 'M.id', '=', 'L.movie')
->select('M.id', 'M.title', 'M.rating', 'M.poster', DB::raw('COUNT(L.id) as C'))
->groupBy('M.id')
->having('C', '>', 0)
->orderByDesc('M.id')
->limit(14)
->get();
I have many projects and each has many orders, some completed, some not.
I want to order them by the amount of completed orders like this:
$products = $products->orderBy(function($product) {
return $product->orders->where('status', 2)->count();
})->paginate(15);
I know the order call doesn't work like this but this is the best was show the problem. SortBy doesn't work because I want to use pagination.
Finally found a good solution for the problem:
$products = $products->join('orders', function ($join) {
$join->on('orders.product_id', '=', 'products.id')
->where('orders.status', '=', 2);
})
->groupBy('products.id')
->orderBy('count', $order)
->select((['products.*', DB::raw('COUNT(orders.product_id) as count')]))->paginate(50);
Try this if it works:
$categories = Prodcuts::with(array('orders' => function($query) {
$query->select(DB::raw('SELECT * count(status) WHERE status = 2 as count'))
$query->orderBy('MAX(count)')
}))->paginate(15);
return View::make('products.index', compact('categories'));
Note: This is not tested.
From 5.2 on wards you can use the withCount for counting relationship result.
In this case the
$products = $products->withCount(['orders' => function ($query) {
$query->where('status', 2);
}]);
Reference : https://laravel.com/docs/5.2/eloquent-relationships
In my controller I have wrote this following code
$usersCount = User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->count();
$locations_array_result = explode(",",$locations_array_result);
foreach ($locations_array_result as $param)
{
$usersCount = $usersCount->whereHas('location', function($q) use($param){
$q->where('location_id', '=', $param );
});
}
This code giving following error
Call to a member function whereHas() on a non-object
Can anyone help me to find out what i have done wrong!!!
$usersCount is already a number from the 1st line of your sample.
You want instead to replace $usersCount->whereHas with User::whereHas in your foreach loop.
Taking a very wild guess here, I would think you need to get all users with these requirements
->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)
plus having a location_id value which exists on an array named $locations_array_result
If this is the case, this is all you need:
User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->whereIn('location_id', $locations_array_result)->get();
EDIT
Following your comment below, I assume user has many to many relation with locations (defined in your model), so eager loading and then using a condition with a callback should do the job:
$users = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->with(array('location' => function($query) use ($locations_array_result)
{
$query->whereIn('location_id', $locations_array_result);
}))->get();