I can query where relationships exist by using the whereHas method, but now I need to get the inverse of this result, where the result does not match the result in the whereHas clause.
Here's my query:
$query->whereHas('actions', function ($query) {
$query->where('actions.created_at', '>', Carbon::now()->addDays(-30));
});
This gets things that are actioned in the last 30 days, but I need to get the things that are NOT actioned in the last 30 days.
It seems like I need to get the max(actions.created_at) from the relationship and see if that value is > 30 days ago, but I'm not sure how I can do this with eloquent.
Note: the relationship between person and action is 1 to many, so there could be multiple action records linked, so I can't just flip the operator to be a "<="
Remember whereHas has more than two parameters:
$query->whereHas('actions', function ($query) {
$query->where('actions.created_at', '>', Carbon::now()->addDays(-30));
}, '=',0);
As a matter of fact, it has two more, but by default it is set to '>=' and '1'. So we add the parameters '=' and '0' (or '<', and '1' for what it matters) to convert it in a subquery like 'all the actions that are not in the subset of actions added in less than 30 days).
whereHas method: http://laravel.com/api/4.1/Illuminate/Database/Eloquent/Builder.html#method_whereHas
Can't you just change your where query to a smaller or equal than?
$query->whereHas('actions', function ($query) {
$query->where('actions.created_at', '<=', Carbon::now()->addDays(-30));
});
Try this [Pseudo Code]:
$actions = App\Models\Action::all();
foreach($actions as $actionArray){
$actionCreatedAt = new Carbon($actionArray->created_at);
$now = Carbon::now();
$difference = $actionCreatedAt->diff($now)->days;
if($difference>30){
$exceedingThirty[] = $actionArray;
} else {
continue;
}
}
Then, you can use $exceedingThirty array, in your view.
OR
Try this:
$sql = "DATEDIFF(actions.created_at, '".date('Y-m-d')."' ) > ". 30;
return App\Models\Action::whereRaw( $sql )->get();
See, if that helps.
Related
Affiliate has many affiliatesHistory, affiliatesHistory belongs to affiliate, how to make the following query?
Take affiliates, where has affiliatesHistory, if affiliatesHistory records count is equal to 1, then do not take affiliatesHistory, which has status of uninstalled.
$affiliates = $user->affiliates()
->whereDoesntHave('affiliatesHistory', function ($q) {
$q->where('affiliates_histories.status', 'Installed earlier')
->orWhere('affiliates_histories.status', 'Uninstalled / Installed earlier');
The following query works, but I need to not take those affiliates, where affiliatesHistory count is equal to 1 and the status is uninstalled.
Any help will be appriaciated.
So, for what I understand you want to get the affiliates which affiliatesHistory status is Installed earlier. If this is the case then try this:
$user_affiliates = $user->affiliates();
$affiliates = $user_affiliates->whereHas('affiliatesHistory', function($q){
$q->where('status', 'Installed earlier');
})->get();
dd($affiliates);
For your case if there are more than one affiliatesHistory items then return else if there is only one affiliatesHistory then it should not contain Uninstalled status, I guess you can use conditional count to get desired results as
$affiliates = Affiliate::withCount([
'affiliatesHistory',
'affiliatesHistory as affiliatesHistoryUninstalled_count' => function ($query) {
$query->where('status', 'Uninstalled');
}
])->where('user_id', $user->id)
->havingRaw('affiliatesHistory_count > 1 OR (affiliatesHistory_count = 1 AND affiliatesHistoryUninstalled_count = 0)')
->get();
I'm building search functionality for my app. To simplify things:
there are two tables: shops and subscriptions
Each shop can have multiple subscription records, subscription has field expires_at. Now, I assume that shop has active subscription if subscription exsists and at least one of shop's subscripion expires_at date is bigger than now().
It is one of the conditions to the whole query. Here is code:
$shops = Shop::when($subscription, function($query, $subscription) {
$query->doesntHave('subscriptions')->orWhereHas('subscriptions', function($q) use ($subscription, $query) {
$query->where('expires_at', '<', now());
});
});
It doesn't work as expected because if i.e. shop has three related subscriptions and at least one of them is expired – it assumes that shop has no active subscription (even though it has).
I would need to implement some nested function inside or whereHas, I guess, to sort by expires_at desc and then limit to one and only then pass where expires_at clause, however I've no idea how.
And I rather need to stick with Eloquent Query Builder rather than DB facade or raw sql.
Basically, it is the same problem what wasn't answered here:
https://laracasts.com/discuss/channels/eloquent/latest-record-from-relationship-in-wherehas?page=1
Try this:
$shops = Shop::doesntHave('subscriptions')->orWhereHas('subscriptions', function ($query) {
$query->whereDate('expires_at', '<', now());
})->get();
try this :
$shops = Shop::WhereHas('subscriptions')->withCount('subscriptions as
active_subscriptions_count' => function ($query) {
$query->where('expires_at', '<', now());
}])->having('active_subscriptions_count', '>=', 3)->get();
Ok, so after some tries in raw sql I figured it out:
$query->select('shops.*')
->leftJoin('subscriptions', 'shops.id', 'subscriptions.shop_id')
->whereNull('subscriptions.id')
->orWhere('subscriptions.expires_at', '<', now())
->where('subscriptions.id', function($q) {
$q->select('id')
->from('subscriptions')
->whereColumn('shop_id', 'shops.id')
->latest('expires_at')
->limit(1);
});
It is not only faster than where exists clause but also gives me what I needed – only the "highest" subscription for given shop is under consideration.
Assume we have an agents table with a quota column and a many-to-many relationship to tickets. With Laravel Eloquent ORM, how can I select only agents having less or equal number of 'tickets' than their 'quota'?
Eager-loading objects must be avoided.
class Agent extends Model {
public function tickets()
{
return $this->belongsToMany(Ticket::class, 'agent_tickets')->using(AgentTicket::class);
}
public function scopeQuotaReached($query)
{
// Does not work. withCount is an aggregate.
return $query->withCount('tickets')->where('tickets_count', '<=', 'quota');
// Does not work. Tries to compare against the string "quota".
return $query->has('tickets', '<=', 'quota');
}
}
Is there a more eloquent (pun intended) way to solve this than using a DB::raw() query with joining and grouping and counting manually?
EDIT
Works:
$query->withCount('tickets')->having('tickets_count', '<=', DB::raw('quota'))->get();
Works:
$query->withCount('tickets')->having('tickets_count', '<=', DB::raw('quota'))->exists();
Breaks: (throws)
$query->withCount('tickets')->having('tickets_count', '<=', DB::raw('quota'))->count();
RELATED
https://github.com/laravel/framework/issues/14492
Issue is closed, links to #9307, I have posted there. Will follow up.
Derived columns like tickets_count can only be accessed in the HAVING clause.
Since there is no havingColumn() method, you'll have to use a raw expression:
$query->withCount('tickets')->having('tickets_count', '<=', DB::raw('quota'));
At a database level I don't know how to achieve this, but you could do it at a Collection level.
// Get users
$agents = Agent::withCount('tickets')->get();
// filter
$good_agents = $agents->filter(function ($agent, $key) {
return $agent->tickets_count >= $agent->quota;
})
->all();
Of course you can inline it:
$good_agents = Agent
::withCount('tickets')
->get()
->filter(function ($agent, $key) {
return $agent->tickets_count >= $agent->quota;
})
->all();
I'm running this code on Laravel. I'm adding filters/ordering if I receive them and I'm altering the query before running it and then paginate the results.
$aDatas = DB::table('datas');
if (!empty($aLocations)) {
foreach ($aLocations as $oLocation) {
$aDatas->orWhere('pc', '=', $oLocation->pc);
}
}
if (!empty($oFilters->note)) {
$aDatas->where('note', '>=', $oFilters->note);
}
if (!empty($oFilters->nb_comments)) {
$aDatas->where('nb_comments', '>=', $oFilters->nb_comments);
}
if (!empty($oOrder->type)) {
$aDatas->orderBy($oOrder->type, $oOrder->sort);
}
// echo $aDatas->where('note', '>=', 5)->count() ????
It's working fine.
But I'd like to use these results to count several parts of it.
The last line shows what I tried to do, counting how many rows in these filtered results have a note >= 5. But doing this will actually filter my original data.
I thought about assigning $aDatas to another variable and then count on this, but I'll have many counts and that seems dirty.
Is there a sweet way to do this ?
Just save your datas an replace the last line with this:
$datas =$aDatas->where('note', '>=', 5)->get();
echo $datas->count();
//use datas here for more action.
For all of your requirement, you might want to resolve in making several queries because a single query will not be able to do that(based from what I know)
//this is to get your total of note greater than 5
$query = DB::table('datas');
$query->where('note', '>=', 5);
$data = $query->paginate(10);
$count = $data->getTotal();
to get your other data
If you are working with pagination, use getTotal() instead
$query = DB::table('datas');
$query->select(
DB::raw('COUNT(stars) AS count'),
DB::raw('starts'),
);
$query->where('notes', '>=', 5);
$query->groupBy('stars');
$data = $query->get();
I want to query my Laravel model using eloquent for results that may need to match some where clauses, then take and skip predefined numbers.
This isn't a problem in itself, but I also need to know the number of rows that were found in the query before reducing the result set with take and skip - so the original number of matches which could be every row in the table if no where clauses are used or a few if either is used.
What I want to do could be accomplished by making the query twice, with the first omitting "->take($iDisplayLength)->skip($iDisplayStart)" at the end and counting that, but that just seems messy.
Any thoughts?
$contacts = Contact::where(function($query) use ($request)
{
if (!empty($request['firstname'])) {
$query->where(function($query) use ($request)
{
$query->where('firstname', 'LIKE', "%{$request['firstname']}%");
});
}
if (!empty($request['lastname'])) {
$query->where(function($query) use ($request)
{
$query->where('lastname', 'LIKE', "%{$request['lastname']}%");
});
}
})
->take($iDisplayLength)->skip($iDisplayStart)->get();
$iTotalRecords = count($contacts);
You can use count then get on the same query.
And by the way, your whole query is a bit over complicated. It results in something like this:
select * from `contacts` where ((`firstname` like ?) and (`lastname` like ?)) limit X, Y
Closure in where is used to make a query like this for example:
select * from table where (X or Y) and (A or B);
So to sum up you need this:
$query = Contact::query();
if (!empty($request['firstname'])) {
$query->where('firstname', 'like', "%{$request['firstname']}%");
}
if (!empty($request['lastname'])) {
$query->where('lastname', 'like', "%{$request['lastname']}%");
}
$count = $query->count();
$contacts = $query->take($iDisplayLength)->skip(iDisplayStart)->get();
The Collection class offers a splice and a count method which you can take advantage of.
First you would want to get the collection..
$collection = $query->get();
Then you can get the count and splice it.
$count = $collection->count();
$records = $collection->splice($iDisplayStart, $iDisplayLength);
This might be hard on performance because you are querying for the entire collection each time rather than putting a limit on the query, so it might be beneficial to cache the collection if this page is going to be hit often. At the same time though, this will hit the database only once, so it's a bit of a trade off.