Laravel query different result between (identical?) two vs three argument 'where' clause - laravel

I'm having a very odd issue with a query in laravel (5.2) - I've got a collection created from some external source (an API), and I'm trying to run a 'where' query to extract specific records.
Originally, I was trying to extract all entries which were submitted during the current month (so, after the first day of this month)
$entries is the starting collection (time entries on a project - see end of post)
$thisMonthStart = (new Carbon('first day of this month'))->toDateString();
//value of this is 2017-02-01, and the issue is not resolved if I remove toDateString()
$entriesThisMonth = $entries->where('spent-at', '>', $thisMonthStart);
//returns an empty collection, but should have 15 results
Now the really odd part, is that I tried instead to get $entries where 'spent-at' is equal to the first day of the month - there should be one entry. If I don't explicitly specify the comparison operator, I get my expected result:
$entriesThisMonth = $entries->where('spent-at', $thisMonthStart);
//one $entries returned, see end of post
However if I specify the = operator
$entriesThisMonth = $entries->where('spent-at', '=', $thisMonthStart);
//empty collection returned
So I'm now very confused - presumably something is wrong in my original collection, but why does the specifying vs not specifying the operator make any difference? I would have thought that those two queries would give identical results?
(and obviously, not being able to specify the operator is not very helpful when trying to do a < or > comparison, but I'm mostly just interested in what the actual difference is between those two syntaxes, and so why they give different results?)
I couldn't find any info anywhere on how these two versions of the query work and so if it's expected that they could give different results - I would think that they should be identical, but maybe someone with a deeper understanding could explain what's causing this?
Thank you to anyone who can shed some light on the mystery!
A sample of the $entries collection in case is of any use (just a single record):
(NB there are definitely records from the current month, I know this example is too old)
Collection {#952 ▼
#items: array:367 [▼
175412141 => DayEntry {#958 ▼
#_root: "request"
#_convert: true
#_values: array:16 [ …16]
+"id": "175412141"
+"notes": ""
+"spent-at": "2013-10-03"
+"hours": "0.75"
+"user-id": "595841"
+"project-id": "4287629"
+"task-id": "2448666"
+"created-at": "2013-10-03T18:07:54Z"
+"updated-at": "2013-11-01T12:50:51Z"
+"adjustment-record": "false"
+"timer-started-at": ""
+"is-closed": "false"
+"is-billed": "true"
+"started-at": "10:45"
+"ended-at": "11:30"
+"invoice-id": "3633772"
}
And this is what is returned by the where query without the operator:
Collection {#954 ▼
#items: array:1 [▼
568944822 => DayEntry {#1310 ▼
#_root: "request"
#_convert: true
#_values: array:15 [▶]
+"id": "568944822"
+"notes": "Tweaking formatting on job ads and re shuffling ad order"
+"spent-at": "2017-02-01"
+"hours": "0.25"
+"user-id": "595841"
+"project-id": "4287629"
+"task-id": "2448666"
+"created-at": "2017-02-01T14:45:00Z"
+"updated-at": "2017-02-01T14:45:00Z"
+"adjustment-record": "false"
+"timer-started-at": ""
+"is-closed": "false"
+"is-billed": "false"
+"started-at": "14:30"
+"ended-at": "14:45"
}
]
}

To fix your issue... "returns an empty collection, but should have 15 results". If the collection already exists, you need to filter the results. Something like so:
$thisMonthStart = new Carbon('first day of this month');
$entriesThisMonth = $entries->filter(function ($entry) use ($thisMonthStart) {
return $entry['spent-at'] >= $thisMonthStart;
});

The method illuminate\Support\Collection::where is different to the database collection where, it doesn't take an operator as the second argument.
The where method signature to the collection object you are working with is where(string $key, mixed $value, bool $strict = true)
Your second example with the operator is looking for all elements in the collection that match the string '='.
For further reading on the collection you are working with (not an eloquent collection) look here
To get the 15 results that you are expecting, use the filter method on the collection.
Something along these lines should work:
$entriesThisMonth = $entries->filter (function ($e) use ($thisMonthStart) {
return $e ['spent-at'] > $thisMonthStart;
});

Related

Get result as object not array in laravel

I have this simple sql query
$orders=DB::table('carts')
-
->join('prod_suppliers','carts.prod_supplier_id','=','prod_suppliers.id')
->join('tickets','carts.ticket_id','=','tickets.id')
->distinct()
->select(
'suppliers.id as supplier_id',
'suppliers.supplier',
'suppliers.house_no_street_no',
'barangays.brgy_name',
'suppliers.city_id',
'tickets.id',
'tickets.ticket_no',
)
->get();
dd($orders);
i want my result to be an object but im getting result as an array
Collection {#525 ▼
#items: array:2 [▶]
}
what is the best way i can do to solve this problem?
It is a collection of Order objects. The dd() shows array, but those are objects inside that array. If you loop on the $orders variable, you will have ability to use object notation to get the fields you need.
For example:
foreach($objects as $object){
echo ($object->id); // or whatever field you need from the $order object
}
This is actually where Laravel really shines though - it has a fantastic model system. Using a model to pull the data through a simple Eloquent query, along with any relationships you need, might make your like far easier.

Laravel Custom Collections

I'm trying to understand something. Consider the following:
$collection = collect(['example1' => 'test1', 'example2' => 'test2']);
When I do the following, I end up with this result:
Collection {#867 ▼
#items: array:2 [▼
"example1" => test1
"example2" => test2
]
}
What I want to be able to do is this:
echo $collection->example1 // Should display test1
But instead it says "Trying to get property of non object".
So, I have two questions:
Can somebody explain the above behaviour?
Can somebody help with a solution so I can do $collection->example1?
Collections are objects containing an array. If you look back at what you posted you can see that you have an object of class Collection that contains an item which is an array. You can access the array items in normal array syntax or using the object's getter.
$product->get('subscription'); //object oriented way
$product['subscription']; //access as an array item

Laravel 5 - looping over a Collection

can't seem to figure this out. I have the following code
dd($this->project->dsReportingDoc->reportingDocUpload);
if(!empty($this->project->dsReportingDoc->reportingDocUpload)) {
dd("TEST");
foreach($this->project->dsReportingDoc->reportingDocUpload as $key){
}
}
Now the first dd prints out something like the following
Collection {#274 ▼
#items: array:2 [▼
0 => ReportingDocUpload {#275 ▶}
1 => ReportingDocUpload {#276 ▶}
]
}
So, there are two items in the Collection. However, the second dd never seems to get executed, so it must never make it into the if statement.
If anything is in the collection, I need to loop them and get a parameter. So I need to see if the item exists first.
Why would my if statement be failing here when it is not empty?
Thanks
The dd() debug function stops execution of the current request. So you can only call it once and get output - see here.
This is the reason your if condition and foreach aren't executing.
Try this
if($this->project->dsReportingDoc->reportingDocUpload) {
dd("TEST");
foreach($this->project->dsReportingDoc->reportingDocUpload as $key){
}
}
What you can do is, assign the
$this->project->dsReportingDoc->reportingDocUpload
to a variable so you don't have to rewrite it every where.

Laravel Eloquent - distinct() and count() not working properly together

So I'm trying to get the number of distinct pids on a query, but the returned value is wrong.
This is what I try to do:
$ad->getcodes()->groupby('pid')->distinct()->count()
what returns the value "2", while the value it should return, should be "1".
As a workaround, I'm doing this:
count($ad->getcodes()->groupby('pid')->distinct()->get())
what works fine and returns "1"
Is there any rule where count and distinct cannot be on the same query? I find the workaround kind of "heavy", I would like to make the original query work :(
The following should work
$ad->getcodes()->distinct()->count('pid');
A more generic answer that would have saved me time, and hopefully others:
Does not work (returns count of all rows):
DB::table('users')
->select('first_name')
->distinct()
->count();
The fix:
DB::table('users')
->distinct()
->count('first_name');
Anyone else come across this post, and not finding the other suggestions to work?
Depending on the specific query, a different approach may be needed. In my case, I needed either count the results of a GROUP BY, e.g.
SELECT COUNT(*) FROM (SELECT * FROM a GROUP BY b)
or use COUNT(DISTINCT b):
SELECT COUNT(DISTINCT b) FROM a
After some puzzling around, I realised there was no built-in Laravel function for either of these. So the simplest solution was to use use DB::raw with the count method.
$count = $builder->count(DB::raw('DISTINCT b'));
Remember, don't use groupBy before calling count. You can apply groupBy later, if you need it for getting rows.
You can use the following way to get the unique data as per your need as follows,
$data = $ad->getcodes()->get()->unique('email');
$count = $data->count();
Hope this will work.
I had a similar problem, and found a way to work around it.
The problem is the way Laravel's query builder handles aggregates. It takes the first result returned and then returns the 'aggregate' value. This is usually fine, but when you combine count with groupBy you're returning a count per grouped item. So the first row's aggregate is just a count of the first group (so something low like 1 or 2 is likely).
So Laravel's count is out, but I combined the Laravel query builder with some raw SQL to get an accurate count of my grouped results.
For your example, I expect the following should work (and let you avoid the get):
$query = $ad->getcodes()->groupby('pid')->distinct();
$count = count(\DB::select($query->toSql(), $query->getBindings()));
If you want to make sure you're not wasting time selecting all the columns, you can avoid that when building your query:
$query = $ad->select(DB::raw(1))->getcodes()->groupby('pid')->distinct();
I came across the same problem.
If you install laravel debug bar you can see the queries and often see the problem
$ad->getcodes()->groupby('pid')->distinct()->count()
change to
$ad->getcodes()->distinct()->select('pid')->count()
You need to set the values to return as distinct. If you don't set the select fields it will return all the columns in the database and all will be unique. So set the query to distinct and only select the columns that make up your 'distinct' value you might want to add more. ->select('pid','date') to get all the unique values for a user in a day
Based on Laravel docs for raw queries I was able to get count for a select field to work with this code in the product model.
public function scopeShowProductCount($query)
{
$query->select(DB::raw('DISTINCT pid, COUNT(*) AS count_pid'))
->groupBy('pid')
->orderBy('count_pid', 'desc');
}
This facade worked to get the same result in the controller:
$products = DB::table('products')->select(DB::raw('DISTINCT pid, COUNT(*) AS count_pid'))->groupBy('pid')->orderBy('count_pid', 'desc')->get();
The resulting dump for both queries was as follows:
#attributes: array:2 [
"pid" => "1271"
"count_pid" => 19
],
#attributes: array:2 [
"pid" => "1273"
"count_pid" => 12
],
#attributes: array:2 [
"pid" => "1275"
"count_pid" => 7
]
$solution = $query->distinct()
->groupBy
(
[
'array',
'of',
'columns',
]
)
->addSelect(
[
'columns',
'from',
'the',
'groupby',
]
)
->get();
Remember the group by is optional,this should work in most cases when you want a count group by to exclude duplicated select values, the addSelect is a querybuilder instance method.
Wouldn't this work?
$ad->getcodes()->distinct()->get(['pid'])->count();
See here for discussion..
Distinct do not take arguments as it adds DISTINCT in your sql query, however, you MAY need to define the column name that you'd want to select distinct with. Thus, if you have
Flight->select('project_id')->distinct()->get() is equialent to SELECT DISTINCT 'project_id' FROM flights and you may now add other modifiers like count() or even raw eloquent queries.
Use something like this
DB::table('user_products')->select('user_id')->distinct()->pluck('user_id')->toArray();
This was working for me so
Try This:
$ad->getcodes()->distinct('pid')->count()
try this
$ad->getcodes()->groupby('pid')->distinct()->count('pid')

How to convert iqueryable<string> to string in linq?

i have the following code snippet.
in which i just want to return PartyName as a string.
but i get the error:"Cannot implicity convert type 'System.Linq.Iqueryable to string"
if i want to return only string then what to do?
please help me.
return objDatabase.FAPARs
.Where(f => (f.PARTY_CODE == "P003"))
.Select(f => f.PARTY_NAME);
An IQueryable<string> represents a query which could return any number of strings. You want one string - so you need to decide what to do in various situations:
What do you want to happen if the query has no results?
What do you want to happen if the query has one result? (I assume this is simple :)
What do you want to happen if the query has more than one result?
The set of methods which allow you to determine all of this are:
Single - fail if there isn't exactly one result
SingleOrDefault - fail if there's more than one result, return null if there are no results
First - fail if there are no results, return the first of many
FirstOrDefault - return null if there are no results, or the first of many
Last - fail if there are no results, return the last of many
LastOrDefault - return null if there are no results, or the last of many
In each case, "fail" means "throw an exception". (IIRC it's always InvalidOperationException, at least in LINQ to Objects, but I could be wrong.)
So if you're querying by an ID which must exist (i.e. it's a bug if it doesn't) then Single is probably appropriate. If you're querying by an ID which may not exist, then use SingleOrDefault and check whether the return value is null. If you're not querying by an ID, you probably want to use FirstOrDefault or just iterate over the results.
(Note that the default value being null is due to this being a query returning strings, and string being a reference type. In general it's the default value of the element type - so if you had an IQueryable<int>, the default returned would be 0.)
Try
return objDatabase.FAPARs .Where(f => (f.PARTY_CODE == "P003")) .Select(f => f.PARTY_NAME).SingleOrDefault();
Try this:
return objDatabase.FAPARs.Where(f => (f.PARTY_CODE == "P003")).Single(f => f.PARTY_NAME);
return objDatabase.FAPARs.FirstOrDefault(f => f.PARTY_CODE.Equals("P003")).PARTY_NAME
return objDatabase.FAPARs.OfType<FAPAR>()
.Where(f => (f.PARTY_CODE == PartyCode && f.COMP_NO == ComCode))
.Select(f => f.PARTY_NAME).SingleOrDefault();

Resources