laravel collections->keyBy finds similar keys - php

laravel 5.5
here is the collections:
$collections = collect(['name' => 'Rob'], ['nickname' => 'Robby']);
the both methods:
$collections->keyBy('name')
$collections->keyBy('nickname')
return equil result
Collection {#246 ▼
#items: array:1 [▼
"" => "Rob"
]
}
as me this looks like wrong...

I Think You Should pass one parameter as an array .. Try This
$collections = collect([['name' => 'Rob'], ['nickname' => 'Robby']]);

Your problem is that your collection is not well formatted, do it like this:
$collections = collect([ //main collection array
[ 'name' => 'Rob1', 'nickname' => 'Robby1' ] //object 0 inside collection array with well formated keys => values
]);
Now when you use:
$collections->keyBy('name')
$collections->keyBy('nickname')
It will work as spected

so results are
Collection {#246 ▼
#items: array:2 [▼
"Rob" => array:1 [▼
"name" => "Rob"
]
"" => array:1 [▼
"nickname" => "Robby"
]
]
}
and
Collection {#246 ▼
#items: array:2 [▼
"" => array:1 [▼
"name" => "Rob"
]
"Robby" => array:1 [▼
"nickname" => "Robby"
]
]
}
still looks invalid searching in collections...
and
$collections->keyBy('nick')
returns
Collection {#246 ▼
#items: array:1 [▼
"" => array:1 [▼
"nickname" => "Robby"
]
]
}

Related

How to Merge the results of a $match and $geoNear in one pipeline in Mongodb

I have a collection of restaurants and I want to query them with two diffrent criterias and merge them together
I have two aggregation pipelines one that is a $match and the other one that is a $geoNear ,
array:1 [▼
0 => array:1 [▼
"$match" => array:1 [▼
"$or" => array:1 [▼
0 => array:1 [▼
"$and" => array:2 [▼
0 => array:1 [▼
"country" => array:1 [▶]
]
1 => array:1 [▼
"$or" => array:1 [▼
0 => array:1 [▼
"$or" => array:1 [▶]
]
]
]
]
]
]
]
and
^ array:1 [▼
0 => array:1 [▼
"$geoNear" => array:5 [▼
"near" => array:2 [▶]
"distanceField" => "distance.calculated"
"spherical" => true
"key" => "location"
"query" => array:1 [▶]
]
]
]
I want to be able to make just one call/ in one pipeline and get ALL NON DUPLICATES restaurants. (a way to identify them it would be with the _id )
For example getting all the ones that match $match pipeline , the same with $geoNear , merge them and remove duplicates
You can use query option in $geoNear. It will include the documents which matches the condition in query.
Reference
Sample from documentation:
db.places.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
distanceField: "dist.calculated",
maxDistance: 2,
query: { category: "Parks" }, //Query portion
includeLocs: "dist.location",
spherical: true
}
}
])

in laravel collection remove key if value is null

i new in laravel, trying to filter null value array and remove from array.
like this
i have this array from response
array:8 [▼
"videoa" => Collection {#801 ▼
#items: []
}
"music" => Collection {#872 ▼
#items: array:69 [▶]
}
"meditation" => Collection {#869 ▼
#items: array:1 [▶]
}
]
see , in this array videos has null , but i want following array
array:8 [▼
"music" => Collection {#872 ▼
#items: array:69 [▶]
}
"meditation" => Collection {#869 ▼
#items: array:1 [▶]
}
]
when the key value is null then remove it from array.
i tried array_filter($array);
but not get any results.
plase help me

Laravel using unique for collections in relationships

getting data from database by this code:
$allTickets = Ticket::with(['user','replies','supporters'])->whereUserId(auth()->user()->id)->get();
return this result for me which that have 2 index in array and all of this items i have supporters relationship which both are same, now my question is how can i use unique in collections
Illuminate\Database\Eloquent\Collection {#1550 ▼
#items: array:2 [▼
0 => Modules\Ticket\Entities\Ticket {#1548 ▼
#guarded: array:1 [▶]
#connection: "mysql"
#table: "tickets"
...
#relations: array:3 [▼
"user" => App\User {#1575 ▶}
"replies" => Illuminate\Database\Eloquent\Collection {#1565 ▶}
"supporters" => Illuminate\Database\Eloquent\Collection {#1572 ▼
#items: array:1 [▼
0 => App\User {#1585 ▼
...
#attributes: array:19 [▼
"id" => 1
"user_id" => null
...
"api_token" => "a"
"remember_token" => null
"deleted_at" => null
"created_at" => "2020-06-14 13:56:45"
"updated_at" => "2020-06-14 13:56:45"
]
...
}
]
}
]
...
}
1 => Modules\Ticket\Entities\Ticket {#1567 ▼
#guarded: array:1 [▶]
#connection: "mysql"
#table: "tickets"
...
#relations: array:3 [▼
"user" => App\User {#1575 ▶}
"replies" => Illuminate\Database\Eloquent\Collection {#1551 ▶}
"supporters" => Illuminate\Database\Eloquent\Collection {#1559 ▼
#items: array:1 [▼
0 => App\User {#1585 ▼
...
#attributes: array:19 [▼
"id" => 1
"user_id" => null
...
"api_token" => "a"
"remember_token" => null
"deleted_at" => null
"created_at" => "2020-06-14 13:56:45"
"updated_at" => "2020-06-14 13:56:45"
]
...
}
]
}
]
...
}
]
}
my tested code:
#foreach($allTickets as $supporter)
#php
$unique = $supporter->supporters->unique();
#endphp
#foreach($unique->values()->all() as $user)
...
...
...
#endforeach
#endforeach
Do not try to do unique on the collection, try to get your data already filtered from your database. You could group by any of your relationships attributes, just pass in a closure to handle the query for the eager-loaded content, something like:
$allTickets = Ticket::with(['user','replies'])->with([
'supporters' => function($query) {
$query->groupBy('id');
}
])->whereUserId(auth()->user()->id)->get();
Or you can query like this:
$allTickets = collect(Ticket::with(['user','replies','supporters'])->whereUserId(auth()->user()->id)->get());
$allUniqueTickets = $allTickets->unique();
$tickets = $allUniqueTickets->values()->all();
That's how you can easily get all the unique values from your controller. I hope this answers your question.
For more information, you can check https://laravel.com/docs/7.x/collections#method-unique

php retrieve multidimensional array value

This is my sample array value returned
array:3 [▼
"Status" => array:1 [▼
0 => "200"
]
"Data" => array:1 [▼
0 => array:1 [▼
0 => array:2 [▼
"sellerId" => array:1 [▼
0 => "TEST01"
]
"currency" => array:1 [▼
0 => "USD"
]
]
]
]
"Date" => array:1 [▼
0 => "Dec 31 2019"
]
]
My sample code to retrieve the value from the array above
foreach($json_array as $key => $json) {
if($key == "Status") {
$status = $json[0];
} else if ($key == "Date") {
$date = $json[0];
} else {
dd($json[0][0]['sellerId'][0]);
}
}
I am using the method above to retrieve the value from multidimensional array. Is there any better approaches that i can use to achieve my way?
Just do :
$status = $json_array['Status'];
$date = $json_array['Date'];
Maybe you should add some context to your question.

Filtering with Laravel

In my Laravel project I have the following problem. (I have reduced the output a little for easier readability)
I fetch all the posts from a table named Newsposts. I store this as a collection called $all_posts, which looks like this:
Collection {#216 ▼
#items: array:7 [▼
0 => NewsPost {#227 ▶}
1 => NewsPost {#228 ▶}
2 => NewsPost {#229 ▼
#attributes: array:6 [▼
"id" => 6
"title" => "Sample title"
"body" => "<p>Some html</p>"
"user_id" => 15
]
#original: array:6 [▶]
#casts: []
.
.
#guarded: array:1 [▶]
}
3 => NewsPost {#230 ▶}
4 => NewsPost {#231 ▶}
]
}
I then fetch all entries from another table called user_read witch contains all the posts the current user have read. This table contains a reference to the user with a 'user_id'-column, and a reference to the newsposts with a 'post_id'-column. I also store this in a collection, which looks like this:
Collection {#219 ▼
#items: array:2 [▼
0 => UserRead {#210 ▼
#attributes: array:4 [▼
"user_id" => 15
"post_id" => 6
]
#original: array:4 [▶]
#casts: []
.
.
#guarded: array:1 [▶]
}
1 => UserRead {#217 ▶}
]
}
So what I want to do now is take the $all_posts-collection and remove all the posts from $read_posts-collection, and store it in a new collection. Lets call it $unread_posts. How can i achieve this?
get all readed post id first, then use filter function to filter unread posts
$readedPosts = $user_read->pluck('post_id');
$unread_posts = $all_posts->filter(function($value) use ($readedPosts) {
return !in_array($value->id, $readedPosts);
})

Categories