Using count in an eloquent query - laravel

$data = User::where(function($q){
$q->where(User::whereIn('access', array(1,2,3,4))->count(), 4);
})->get();
I only want users who have an access of 1,2,3,4 (used this for illustrative purposes but this is usually a passed in array).
I am getting all users where the count of a where in query equals 4.
How can this be done in eloquent? My above code errors, as it is trying to select all from users where 4 = 4.
Where am I going wrong?

Related

count get collection and count query giving different results

So i am trying to get distinct collections like this
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->get();
But when i do a count($data) I get 1600 but when i run
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->count();
I get 1440. This is weird as i only want collection with distinct field 'Farmer_BankVerificationNumber_Farmer'. How do i write the query correctly?
It's because you're asking your query to count the distinct values in the wrong place.
You need to tell your count parameter what field you would like to count on. So you'll basically ask the query to get the database information, separate the distinct values and then count how many of a specific field are distinct.
Your finished query should look like this:
$data = DB::table('project_1_data')
->distinct()
->count('Farmer_BankVerificationNumber_Farmer');

Laravel - How to paginate united Many To Many(Polymorphic) collection?

Trying to figure out how to fetch two related models(obviously united) of my Many To Many(Polymorphic) relationship.
What we have:
3 models: Bucket, Template and DesignPack.
Bucket has Many-To-Many(Polymorphic) relationship with Template and DesignPack(It means we have pivot table bucketables).In essence Bucket can have(be related with) both: Template and DesignPack.
Laravel 6.*
What I want to get:
I want to get a Bucket templates and design packs united in one collection and paginated!
Please check one of the solutions I've tried:
$templates = Bucket::find($bucket_id)->templates()->select(['id', 'file_name as name', 'size', 'preview']);
$design_packs = Bucket::find($bucket_id)->dps()->select(['id', 'name', 'size', 'preview']);
$all = $templates ->union($design_packs )->paginate(10);
Unfortunately that solution throws me the error(thought I checked what each request returns and it returns the same fields, not different):
"SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select `id`, `size`, `preview`, `bucketables`.`bucket_id` as `pivot_bucket_id`, `bucketables`.`bucketable_id` as `pivot_bucketable_id`, `bucketables`.`bucketable_type` as `pivot_bucketable_type` from `design_packs` inner join `bucketables` on `design_packs`.`id` = `bucketables`.`bucketable_id` where `bucketables`.`bucket_id` = 3 and `bucketables`.`bucketable_type` = App\DesignPack and `design_packs`.`deleted_at` is null) union (select `id`, `size`, `preview` from `templates` inner join `bucketables` on `templates`.`id` = `bucketables`.`bucketable_id` where `bucketables`.`bucket_id` = 3 and `bucketables`.`bucketable_type` = App\Template and `templates`.`deleted_at` is null))"
Are there any different way to get what I want?
May be examples, documentation links or any helpful ideas?
Will be so grateful guys for any help!
Thank you!
You can pass closure to queries:
$templates = Bucket::whereHas('templates', function($query) use $bucket_id {
$query->where('bucket_id', $bucket_id);
})->get();
$designPacks = Bucket::whereHas('dps', function($query) use $bucket_id {
$query->where('bucket_id', $bucket_id);
})->get();
then merge 2 eloquent collections:
$mergedCollections = $templates->merge($designPacks);
now you have a collection of both results, you can select specific fields, limit the results or etc. you may want take a look at Laravel collection helpers.
also if you insist to use the union, you may want to take a look at this treat:
The used SELECT statements have a different number of columns (REDUX!!)

Laravel Eloquent: count($result) vs $result->count()

I'm a bit new to this, and was originally trying to check if my model was returning results with isEmpty(), but thought I'd try count() instead, then I came across the following:
I've got the following code, which returns data from my model:
$results = Game::where('code', '=', $code)->with('genre', 'creator')
And whether I use first() or get() combined with count(result) or $results->count() I get different values, and I'm not sure why.
when using ->first()
dd($results->count()) = 11930 // Number of rows in the db
when using ->get()
dd($results->count()) = 1 // What I'd expect the query to return
when using ->first()
dd(count($results)) = "count(): Parameter must be an array or an object that implements Countable"
when using ->get()
dd(count($results)) = 1
I don't understand 1) why when using first, the count is the same as every row in the db. 2) Why count() can't be used with first().
Is anyone able to shed some light as to why I can't use count on first as I'd like to?
Update:
I'm also not able to use ->isEmpty() with ->first() but can with ->get()...?
When I try using it with first, I get Illuminate\Database\Query\Builder::isEmpty does not exist.
Disclaimer: I'm not sure why your database count and your results count aren't the same, however I can shed some light on the different types of count.
Game::where('code', '=', $code)->count();
This is being called on a query builder instance. It is run on the database query, without selecting all the rows. Check out the title Aggregates here: https://laravel.com/docs/5.7/queries
Game::where('code', '=', $code)->get()->count();
As soon as your use get() laravel selects the rows, boots them all as models, and creates a collection. This count is on the collection (a bit like an array) so just gets the number that are returned (i.e. if they are paginated or anything like that it will just get that amount). Check out Count here.
Game::where('code', '=', $code)->first()->count();
This is being run on the first returned model... unless you've written it, a default laravel model won't have a count() method.
count($results)
Finally, count() when not a class method is just the default php function that returns the length of an array or other object (documentation).
First of all,
get() returns collection of objects while first() returns modal object of query.
$results = Game::where('code', '=', $code)->with('genre', 'creator')
dd($results->count()) = 11930 // Number of rows in the db
when using ->get()
$results = Game::where('code', '=', $code)->with('genre', 'creator')->get()
dd($results->count()) = 1
because it has collection, which contains numbers of objects of database data. As ->count getting only one collection so it returns 1.

Optimize query Eloquent laravel for thousands records

I have a problem with a query in Laravel with many records, because it is so slow.
I have a table users that has 4934 records.
I have table of relations that for example is named user_relation_values that has 17482 records.
I have a table values that has 20495 records.
Now I receive,from front end, N id of users, so I could receive 1 id or 4934 ids or 2000 ids or 1000 ids, so I don't know how many ids I will receive.
I have to return, starting by ids received, the relations of model users with id.
So in My backend I have a function like this:
$users= $request->input('users');
$usersValues = array();
foreach ($users as $user){
$o = User::find($owner['id']);
$o->values;
$owersProperties[] = $o;
}
I have the relations in My User model:
public function values(){
return $this->belongsToMany('App\Models\Values');
}
I have put The various indexes on tables but the query Is so slow if I receive the all id.
If I receive all 4934 ids the query takes more than 20 seconds, but I read that someone make query for milions of records in just over 5 seconds.
How can I optimize my table or my query?
Looking at your example code, you’re query users in a loop (n queries), and then also querying values for each user. That’s going to issue an exponential number of queries. Instead, you should query the users and eager-load the values relationship.
If you’re getting an array of user IDs as input, then you can pass that to Eloquent’s find() method:
$users = User::find($request->input('users'));
You should also eager-load the values relationship if you plan on using it in a loop:
$users = User::with('values')->find($request->input('users'));
This should dramatically reduce the number of queries you issue.

Laravel Eloquent and SUM() - Do not consider the decimal

I run in a strange issue, I need to sum a column using eloquent but every function I use eloquent do not consider the decimal.
First of all I have the column set ad DECIMAL(8,2) and in my DB the data is stored in the correct way.
If I just use a simple query:
select sum(total_after_tax) from invoice_details where invoice_id = 1
I get the result: 312.93
Now if I use eloquent:
$invoice = Invoice::with(['details'])->get();
dd($invoice->first()->details->sum('total_after_tax'));
I get: 312.
How is this possibile? How can I get the correct value form DB?

Resources