How to get data according to group in laravel - php

Hello all I am using laravel and I am having some problem below is my array
[
{
"id": "375c572b-5c54-4c91-bb11-dda23bcf8052",
"user_id": "a8dd7771-fc0e-46d3-b5a6-5a9ebaa89b9f",
"group_id": "37b82ef4-c211-49a8-8e1c-a62c0f107219"
},
{
"id": "809ee65e-e5b7-4519-ba7b-9d98ef0a4e3a",
"user_id": "30517691-89b1-4247-9b74-522350e82622",
"group_id": "37b82ef4-c211-49a8-8e1c-a62c0f107219"
},
{
"id": "8d764272-3a5c-402f-8488-a641e3567e3e",
"user_id": "a8dd7771-fc0e-46d3-b5a6-5a9ebaa89b9f",
"group_id": "a24a9b00-a805-4142-99f4-3384bbb842f0"
}
]
from the above array I want the result like below I want data with same group_id
[
{
"id": "375c572b-5c54-4c91-bb11-dda23bcf8052",
"user_id": "a8dd7771-fc0e-46d3-b5a6-5a9ebaa89b9f",
"group_id": "37b82ef4-c211-49a8-8e1c-a62c0f107219"
},
{
"id": "809ee65e-e5b7-4519-ba7b-9d98ef0a4e3a",
"user_id": "30517691-89b1-4247-9b74-522350e82622",
"group_id": "37b82ef4-c211-49a8-8e1c-a62c0f107219"
},
]
below is my code I tried but I am not getting result
$get_group_ids = DB::table('auc_chat_group_users')
->select("id","user_id","group_id")
->whereIn('user_id', $request->user_id)
->orWhere('user_id',auth()->user()->id)
->distinct('group_id')
->groupBy("id","user_id","group_id")
->orderBy('group_id')
->get();

pluck() should solve your problem

Related

How to combine values of multiple fields in doctrine?

How to combine values of multiple fields in doctrine?
$qb = $this->createQueryBuilder('article');
$qb->where('article.visible = 1')
->select('article.id', 'article.slots_type', 'article.skill', 'article.type', 'article.views')
->innerJoin('article.categories', 'category')
->addSelect('category.title AS categories')
->orderBy('article.published_at', 'DESC')
->setMaxResults($limit);
I tried use concat and groupBy, but it didn't give the right result
I have arrays
{
"id": 710,
"slots_type": 2,
"skill": "2",
"type": "1",
"views": 1362,
"categories": "strategy",
},
{
"id": 710,
"slots_type": 2,
"skill": "2",
"type": "1",
"views": 1362,
"categories": "seo",
},
I expected
{
"id": 710,
"slots_type": 2,
"skill": "2",
"type": "1",
"views": 1362,
"categories": "strategy, seo",
},

How to Count Nested Child from single table Laravel

Relation method
User model:
public function children()
{
return $this->hasMany(self::class, 'referrer_id')->with('children');
}
Controller Method:
$user = User::with('children')->where('id',$this->request->UserId)->get();
Response
{
"status": true,
"status_code": "Success",
"message": true,
"payload": {
"profile": [
{
"id": 1,
"referrer_id": null,
"name": "beciwerugy",
"username": "lahaky",
"email": "figo#mailinator.com",
"children": [
{
"id": 3,
"referrer_id": 1,
"name": "asd",
"username": "asde",
"email": "asda#asd.com",
"children": [
{
"id": 4,
"referrer_id": 3,
"name": "asd",
"username": "asde12",
"email": "as12a#asd.com",
]
}
]
}
]
}
]
}
]
}
}
I want to count all nestedchild at once
Currently im getting all nested child in ther list
but i just want count

Set name in json format in php/laravel

How to set name to JSON object in laravel as I want to display for a plugin in Vue js for image gallery. Whenever I pass data I Got this error : Expected Object, got Array.
{
"normal_size": [
{
"id": 1,
"image": "MICMIMC30651-6.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-6.jpg"
},
{
"id": 2,
"image": "MICMIMC30651-61.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-61.jpg"
},
{
"id": 3,
"image": "MICMIMC30651-62.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-62.jpg"
},
{
"id": 4,
"image": "MICMIMC30651-63.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-63.jpg"
}
]
}
output I wanted :
image : {
"normal_size": [
{
"id": 1,
"image": "MICMIMC30651-6.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-6.jpg"
},
{
"id": 2,
"image": "MICMIMC30651-61.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-61.jpg"
},
{
"id": 3,
"image": "MICMIMC30651-62.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-62.jpg"
},
{
"id": 4,
"image": "MICMIMC30651-63.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-63.jpg"
}
]
}
`
Laravel product controller code to pass data to vue component
$normal_size = DB::table('products_images')->where([['products_id', '=', $product]])
->select('products_images.id','products_images.image')
->get()->ToArray();
foreach($normal_size as $value)
{
$value->url = 'http://localhost:8000/images/product/preview/'.$value->image;
}
$output = (array('normal_size' => $normal_size));
return response()->json(($output));
Decode your JSON to convert an array. Format the array structure you want and encode the array to JSON.
$data = '{
"normal_size": [
{
"id": 1,
"image": "MICMIMC30651-6.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-6.jpg"
},
{
"id": 2,
"image": "MICMIMC30651-61.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-61.jpg"
},
{
"id": 3,
"image": "MICMIMC30651-62.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-62.jpg"
},
{
"id": 4,
"image": "MICMIMC30651-63.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-63.jpg"
}
]
}';
$output = json_encode(['image' => json_decode($data, true)]);
echo $output;
Working demo.
SOLUTION:
Just adjust the output format before sending.
$output= ['image' => json_decode($data)];
or
$output= array("image" => json_decode($data));

Rejecting certain index parameters in an array

Given an array, in which each index there are certain key value pairs, how do I select certain key value pairs and reject the others.
$channels = Channel::get()->toArray();
This will yield the following Array:
"channels": [
{
"id": 1,
"name": "Info Release",
"slug": "info-release",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-02 01:23:50",
"updated_at": "2018-12-05 07:54:41"
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-05 05:34:50",
"updated_at": "2018-12-05 07:54:32"
},
{
"id": 12,
"name": "haha",
"slug": "haha",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-29 23:27:16",
"updated_at": "2018-12-29 23:27:16"
}
],
What is the best way to turn that array into this:
"channels": [
{
"id": 1,
"name": "Information Release",
"slug": "information-release",
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
},
{
"id": 12,
"name": "haha",
"slug": "haha",
}
],
So that if I write
$channels[0]->id
it will spit out 1
Can You try this
$viewFileds = ['id', 'name', 'slug'];
$channels = Channel::get()
->map(function ($each) use ($viewFileds) {
return Arr::only($each, $viewFileds);
});
It will return the new Collection by Only the field enabled in the $viewFileds
Hope it helps

Laravel 5.2: extracting nested collection

I am working on Laravel 5.2 application. I'm having an issue in extracting the nested collection from the parent collection. I'm trying to get all the posts from the users whom I am following like this:
$posts = \Auth::user()->following()->get()->each(function($following){
return $following->posts;
});
But it is returning me a collection of the users I am following and the posts collection is nested in this collection, such that:
[
{
"id": 2,
"name": "John Doe",
"username": "john",
"email": "johny#example.com",
"created_at": "2016-03-08 11:06:45",
"updated_at": "2016-03-08 11:06:45",
"pivot": {
"follower_id": 1,
"followee_id": 2
},
"posts": [
{
"id": 1,
"user_id": 2,
"title": "Post title 1",
"created_at": "2016-03-08 11:09:22",
"updated_at": "2016-03-08 11:09:22"
},
{
"id": 3,
"user_id": 2,
"title": "Post title 2",
"created_at": "2016-03-09 08:04:18",
"updated_at": "2016-03-09 08:04:18"
}
]
}
]
How can I get all the posts from the all of the users I'm following as a collection?
Any help will be appreciated
$posts = [];
\Auth::user()->following()->get()->each(function($following) use(&$posts){
array_push($posts, $following->posts)
});
Hope this helps.

Categories