Array_merge in laravel - php

I have data I return them into collection but it does not return as i wish and i need help to fix it.
output
array:11 [▼
0 => Collection {#2762 ▼
#items: array:3 [▼
0 => Product {#2758 ▶}
1 => Product {#2759 ▶}
2 => Product {#2760 ▶}
]
}
1 => Collection {#2792 ▼
#items: []
}
2 => Collection {#2948 ▼
#items: []
}
3 => Collection {#3102 ▼
#items: []
}
4 => Collection {#3216 ▼
#items: []
}
5 => Collection {#2886 ▼
#items: array:10 [▶]
}
6 => Collection {#2920 ▼
#items: array:3 [▶]
}
7 => Collection {#3046 ▼
#items: array:12 [▶]
}
8 => Collection {#3074 ▼
#items: []
}
9 => Collection {#3188 ▼
#items: array:7 [▶]
}
10 => Collection {#3288 ▼
#items: []
}
]
Code
$category = Category::where('slug','=',$catslug)->with('childs', 'parent')->first();
$pp1[] = Product::where('category_id',$category->id)->get();
foreach($category->childs as $first){
$pp2[] = Product::where('category_id',$first->id)->get();
if(count($first->childs)> 0){
foreach($first->childs as $second){
$pp3[] = Product::where('category_id',$second->id)->get();
}
}
}
$products = array_merge($pp1,$pp2,$pp3);
dd($products);
What it should return
It should return all products from all arrays in 1 array only and not categorized by Collection {#2792 ▼ I need to get only collections with items and not the ones that are empty.
Any idea?
update
I've also tried this:
$pp1 = [];
$pp2 = [];
$pp3 = [];
$pp1 = Product::where('category_id',$category->id)->get();
foreach($category->childs as $first){
$pp2 = Product::where('category_id',$first->id)->get();
if(count($first->childs)> 0){
foreach($first->childs as $second){
$pp3 = Product::where('category_id',$second->id)->get();
}
}
}
it returns:
array_merge(): Argument #1 is not an array

You can get all products of the category, of its children and of its grand-children as,
$category = Category::where('slug','=',$catslug)
->with([
'products',
'childs.products',
'childs.childs.products',
'parent'
])
->first()
->toArray();
$products = array_merge([
data_get($category, 'products'),
array_collapse(data_get($category, 'childs.*.products')),
array_collapse(data_get($category, 'childs.*.childs.*.products'))
]);
Fiddle : https://implode.io/ebXrD8

Related

Do you know why it appears "Property [registrationType] does not exist on this collection instance"?

Do you know why the " #if ($nextRegistration->participants->registration_type->contains('certificate_available', 'Y')) [ " shows "Property [registrationType] does not exist on this collection instance"?
I want to show for each registration a link "Get certificate" if the column "certificate_available" of the "registration_types" table has the value "Y".
<ul class="list-group">
#foreach($nextRegistrations as $nextRegistration)
#if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
#if ($nextRegistration->participants->registration_type->contains('certificate_available', 'Y'))
<a href="{{route('conferences.certificateInfo',
[
'regID'=> $nextRegistration->id])}}"
class="btn btn-primary ml-2">Download certificate</a>
#endif
</li>
#endif
#endforeach
</ul>
The participant model has the registration_type():
class Participant extends Model
{
public function registration(){
return $this->belongsTo('App\Registration');
}
public function registration_type(){
return $this->belongsTo('App\RegistrationType');
}
}
To get the $nextRegistrations the code is:
$nextRegistrations = $user->registrations()
->with('participants.registration_type')
->whereHas(
'conference',
function ($query) {
$query->where('end_date', '>', now());
}
)->paginate($pageLimit);
And $nextRegistrations shows like:
LengthAwarePaginator {#336 ▼
#total: 3
#lastPage: 1
#items: Collection {#320 ▼
#items: array:3 [▼
0 => Registration {#308 ▼
...
#relations: array:1 [▼
"participants" => Collection {#327 ▼
#items: array:1 [▼
0 => Participant {#332 ▼
...
#relations: array:1 [▼
"registration_type" => RegistrationType {#337 ▼
#fillable: array:7 [▶]
....
#attributes: array:10 [▼
"id" => 1
"name" => "general"
"description" => "desc general"
"conference_id" => 1
"certificate_id" => 1
"certificate_available" => "Y"
]
...
}
]
...
}
]
}
]
...
}
1 => Registration {#321 ▶}
2 => Registration {#317 ▶}
]
}
....
}
How do you retrieve the data from the database. You may need to use the with method:
$data = Participant::with('registration_type')->get()
or in the view use "registration_type" as a method, not as a property:
$nextRegistration->participants->registration_type()
You can find more about the belongs to relationship here: https://laravel.com/docs/5.6/eloquent-relationships#one-to-many-inverse

How to retrieve nested array data in PHP/Laravel?

{`
+"insights": array:1 [▼
0 => {#209 ▼
+"group": "Provision"
+"dataset": array:1 [▼
0 => {#207 ▼
+"group": "Provision"
+"set": array:3 [▼
0 => {#194 ▼
+"name": "Neutral"
+"value": 917
}
1 => {#203 ▶}
2 => {#197 ▶}
]
}
]
}
]
+"errorCode": 0
}`
How to get the name property inside the set array? I've tried multiple way but it kept error return trying to get property non-object.
suppose you provide $response to your blade view
$response = {
+"insights": array:1 [▼
0 => {#209 ▼
+"group": "Provision"
+"dataset": array:1 [▼
0 => {#207 ▼
+"group": "Provision"
+"set": array:3 [▼
0 => {#194 ▼
+"name": "Neutral"
+"value": 917
}
1 => {#203 ▶}
2 => {#197 ▶}
]
}
]
}
]
+"errorCode": 0
}
You have to loop through to response , in your blade view :
#foreach($response->insights as $insight)
#foreach($insight['dataset'] as $dataset)
#foreach($dataset['set'] as $set)
<tr><td>$set['name']</td></tr>
#endforeach
#endforeach
#endforeach
data_get($data, 'insights.0.dataset.0.set.0.name');
end if you have json - convert it into array -> json_decode(string);
You have to simply loop through it and do whatever you want to with that name,
foreach($response->insights as $temp){
foreach($temp->dataset as $var){
foreach($var as $obj){
$name = $obj->name;
}
}
}

How to array_values laravel collection?

I have a search result from image model and $photo saved the data which $photo->type == 'photo'.
$photo = $image->filter(function($photo,$key) use($path){
if($photo->type == 'photo'){
$photo->url = $path.$photo->image;
return $photo;
}
});
Here is the $photo collection and is there any way to array_values() the items data?
Collection {#352 ▼
#items: array:3 [▼
2 => ImageBanquet {#349 ▶}
3 => ImageBanquet {#350 ▶}
4 => ImageBanquet {#351 ▶}
]
}
Check values() collection helper.
$values = $collection->values();

How to remove the same item from Collection laravel?

I have this collection:
Collection {#604 ▼
#items: array:4 [▼
0 => CarsMark {#596 ▶}
1 => CarsMark {#594 ▶}
2 => CarsMark {#594 ▶}
3 => CarsMark {#595 ▶}
]
}
As you can see there is two the same items (594). How can i retrieve all items from with collection without the same items?
Use the collection's unique method:
$unique = $collection->unique();

How can I get object with query builder in laravel

I'm new to laravel.
In RouteServiceProvider when I'm fetching the article using following code
$router->bind('articles',function($id) {
return \App\Article::active()->findOrFail($id);
});
I get an object:
Article {#323 ▼
#fillable: array:18 [▶]
#dates: array:1 [▶]
...
}
But when I build a custom query using QueryBuilder
public static function getArticleById($id) {
return DB::table('articles')
->leftJoin('news','articles.news_id','=','news.id')
->select('articles.*','reports.name as selected_report_id')
->where('articles.id',$id)
->get();
}
I get array:
array:1 [▼
0 => {#342 ▼
+"id": 40
...
}
]

Categories