Filter a Laravel collection by date - laravel

In the code given below $allZip["assigned_date"] has value 2016-07-27 18:12:26. When I try to compare it with the created_at field which is a timestamp field as well, the result is an empty collection.
$filtered_datas = $datas->filter(function ($data) use($allZip) {
return $data->zip == $allZip["zip"] && $data->created_at < $allZip["assigned_date"];
});
There is data in database with zip field matching value from $allZip["zip"] and created_at field with value 2016-07-19 18:12:26. So it should return one item in the collection but returns an empty collection instead. Why?

Make sure that all your dates is DateTime or Carbon instances, not strings. Otherwise, comparison operator shouldn't working as you expected.

Related

Laravel Collection Filter breaking serialization format

I have a serialized String like this
$string = '[{"name":"FOO"},{"name":""},{"name":"BAR"}]';
I am trying to process it via Laravel Collection's filter method and eliminate items without a defined "name" property.
$collection = collect(\json_decode($string));
$collection = $collection->filter(function($v){
return !empty($v->name);
});
$string = \json_encode($collection->toArray());
dd($string);
Normally I am expecting something like this:
[{"name":"FOO"},{"name":"BAR"}]
But I'm getting something like this:
{"0":{"name":"FOO"},"2":{"name":"BAR"}}
Funny thing is, if I skip the filtering process or return true every time, I keep getting the string in the desired format. Removing the toArray() call has the same result. I don't want to keep the numeric indices as associative object keys.
Why this anomaly? And what should I do to get the serialized data in desired format?
In PHP arrays the index key must be unique.
In your case you have the key 'name' and collection automatically assigns the index key to all items in the collection.
To overcome that problem just call
$string = \json_encode($collection->values());

How to get records where column is not empty or null in laravel?

This is my table:
This is my code I tried:
$socials = SocialIcon::whereNotNull('link')->get()->all();
$socials = SocialIcon::whereNotNull('link')->get();
I want to get all records where column link is not empty.
First you have to understand the difference between NULL and an empty string. NULL is the absence of a value and no memory is allocated for NULL. But empty string is a value with value stored in the memory as "". From your db I can see you have an empty string as a value for the last row in link column. If the value is NULL then you will find NULL is written in the field.
Now as you want to check both NULL and empty you should write it like
$socials = SocialIcon::whereNotNull('link')->orWhere('link','<>','')->get();
this query will both check for NULL and empty value and will return rows with not NULL and empty link value.
Considering a value as NULL same as empty string is not correct. Both are not same.
You can use the below code:
$socials = SocialIcon::where(function($q) {
$q->whereNotNull('link')->orWhere('link','<>','');
})->get();
The resulted query running on DB will be:
select * from social_icons where (link is not null or link <> "")
If you wish to learn more about the Laravel's query builder, click here
You can simply do this
$socials = SocialIcon::where('link', '<>', '')->get();
Read this: https://laravel.com/docs/7.x/queries#where-clauses
Following will also do the job.
$socials = SocialIcon::all();
And then in your template for second case:
#foreach($socials as $social)
#if(!empty($social->link))
your content here
#endif
#endforeach

How to return a query from cosmos db order by date string?

I have a cosmos db collection. I need to query all documents and return them in order of creation date. Creation date is a defined field but for historical reason it is in string format as MM/dd/yyyy. For example: 02/09/2019. If I just order by this string, the result is chaos.
I am using linq lambda to write my query in webapi. I have tried to parse the string and try to convert the string. Both returned "method not supported".
Here is my query:
var query = Client.CreateDocumentQuery<MyModel>(CollectionLink)
.Where(f => f.ModelType == typeof(MyModel).Name.ToLower() && f.Language == getMyModelsRequestModel.Language )
.OrderByDescending(f => f.CreationDate)
.AsDocumentQuery();
Appreciate for any advice. Thanks. It will be huge effort to go back and modify the format of the field (which affects many other things). I wish to avoid it if possible.
Chen Wang.Since the order by does not support derived values or sub query(link),so you need to sort the derived values by yourself i think.
You could construct the MM/dd/yyyy to yyyymmdd by UDF in cosmos db.
udf:
function getValue(datetime){
return datetime.substring(6,10)+datetime.substring(0,2)+datetime.substring(3,5);
}
sql:
SELECT udf.getValue(c.time) as time from c
Then you could sort the array by property value of class in c# code.Please follow this case:How to sort an array containing class objects by a property value of a class instance?

Laravel collections convert key value to key assoc array

I use this function: $assets->pluck('url').
It outputs: ['link','link1','link2']
How to convert it to: [{url: 'link'}, {url: 'link1'}, {url: 'link2'}]
You can add functions to the relation query to only return the url field. This way you will get the desired result.
$assets = auth()->user()->assets()->select('url')->get();
You can use the ->only() collection method to specify the fields you want to return:
$assets = auth()->user()
->assets
->only(['url'])
->all();
To return a collection of which contain only certain field on model you can add an array of list of fields to retrieve as parameters of the get method on your model
ModelName::get(['id', 'url']);
or by passing list of fields as paremter to the select method
ModelName::where([...])->select('id', 'url')->get();

How to get unique year values from created_at?

I have a collection called $products, each instance of which has a field created_at. The latter obviously has a format ('Y-m-d H:i:s') in the DB. Now, it's easy to get instances with unique created_at. However, I'd like to retrieve unique year values of created_at in one single expression (that I can write in my view). What I am looking for is:
$products->unique( year value ('Y' ONLY) of 'created_at' )
This expression should evaluate to something like this: ['2012', '2013', '2016'].
Building on the comment that gives you a collection with unique timestamps, you can map out the year and sort with the following code.
$years = $products->unique(function($item){
return $item['created_at']->year;
})->map(function($item){
return $item['created_at']->year;
})->sort()->toArray();
In addition to Tim result
$products = DB::table('products')->get();
$years = $products->unique(function($item){
return $item['created_at']->year;
})->map(function($item){
return $item['created_at']->year;
})->sort()->toArray();
You can use toArray or values() to get the result of collection

Resources