I am using a query that returns value perfectly but it adds key number to the collection. How can I remove the index number?
Here is the retrieved data
{
"data": {
"0": {
"id": 135,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "hello guys how are you",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 16:56:41",
"updated_at": "2017-10-17 16:56:41"
},
"2": {
"id": 133,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message 2 to user second",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:43:14",
"updated_at": "2017-10-17 15:43:14"
}
}
}
But I want to return results like this
{
"data": [
{
"id": 133,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message 2 to user second",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:43:14",
"updated_at": "2017-10-17 15:43:14"
},
{
"id": 135,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "hello guys how are you",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 16:56:41",
"updated_at": "2017-10-17 16:56:41"
}
]
}
My query is (last few parts that I think is important)
->orderBy('created_at', 'desc')
->get()
->unique('conversation_id');
Please tell me how can I remove "0" and "2" from this collection.
In order to get a collection without a index you have to add the "values" method
$collection->unique('id')->values();
You have to do it manually
$users=\App\User::get()->toArray(); // your query here
$data['data']=[];
foreach($users as $user ){
$data['data'][]=$user;
}
$k=json_encode($data); dd($k);
Related
Hi I have this array code and I need to get: user_id": 4
I just need to get the value 4 from user_id
I have this code but it doesn't work
$myvar = $request['order'][0]['user_id'];
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
"user": {
"success": true,
"data": {
"id": 2,
"auth_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
"name": "Leo",
"email": "ventas#nikostore.net",
"phone": "+51943363373",
"default_address_id": 3,
"default_address": {
"address": "mi dirección",
"house": null,
"latitude": "-1.327080900000001",
"longitude": "-10.6348053",
"tag": null
},
"delivery_pin": "51972",
"wallet_balance": 0,
"avatar": null,
"tax_number": null
},
"running_order": null,
"delivery_details": null
},
"order": [{
"selectedaddons": [{
"addon_category_name": "Comida Marina Ingredientes",
"addon_id": "2",
"addon_name": "Cangrejo Adicional",
"price": "32.00"
}],
"id": 5,
"restaurant_id": 12,
"item_category_id": 2,
"name": "Producto 1 Loquillo",
"price": "423.00",
"old_price": "0.00",
"image": "https://localhost/phpmyadmin/themes/pmahomme/img/logo_left.png",
"is_recommended": 1,
"is_popular": 1,
"is_new": 1,
"desc": "<p>321312</p>",
"placeholder_image": null,
"is_active": 1,
"is_veg": null,
"order_column": null,
"addon_categories": [{
"id": 2,
"name": "Comida Marina Ingredientes",
"type": "SINGLE",
"user_id": 4,
"created_at": "2021-06-10 20:18:37",
"updated_at": "2021-06-10 20:39:18",
"description": null,
"pivot": {
"item_id": 5,
"addon_category_id": 2
},
"addons": [{
"id": 2,
"name": "Cangrejo Adicional",
"price": "32.00",
"addon_category_id": 2,
"user_id": 4,
"created_at": "2021-06-10 20:20:30",
"updated_at": "2021-06-10 20:38:51",
"is_active": 1
}, {
"id": 3,
"name": "Pene",
"price": "95.00",
"addon_category_id": 2,
"user_id": 4,
"created_at": "2021-06-10 23:52:53",
"updated_at": "2021-06-10 23:52:53",
"is_active": 1
}]
}],
"quantity": 1
}],
"coupon": null,
"location": {
"lat": "-5.187080900000001",
"lng": "-80.6348053",
"address": "midreccion",
"house": null,
"tag": null
},
"order_comment": null,
"total": {
"productQuantity": 1,
"totalPrice": 455
},
"method": "COD",
"payment_token": "",
"delivery_type": 1,
"partial_wallet": false,
"dis": 0,
"pending_payment": false,
"tipAmount": null
}
Working from the JSON string you posted here, you need to decode it first with json_decode(). Assuming no flags are set, this will decode to a mix of objects and arrays as represented in the JSON string.
$str = '{"token": "ey...WTg",
"user": {
...
}
}'; //The JSON string you posted in the question
$request = json_decode($str);
From there the path to the user_id is:
$request->order[0]->addon_categories[0]->user_id // 4
Use foreach loop.
Assuming that you have stored array in variable $array.
Code :
foreach($array as $arr)
{
if($arr['user_id'] == 4)
{
$user = $arr;
}
}
You will get the entire object which has user_id 4, you can get related column by
$user_id = $user['user_id];
Hope this will be useful.
I have two JSON Files which I merge with a PHP Script. My PHP script looks like this:
<?php
header('Content-Type: application/json');
$a = file_get_contents("https://example.com/a.json", false);
$b = file_get_contents("https://example.com/b.json", false);
$merge = json_encode(array_merge(
json_decode($a, true),
json_decode($b, true)
));
echo $merge;
?>
My Output looks like this:
[{
"id": 1141,
"iid": 167,
"project_id": 17,
"title": "test",
"description": "test",
"state": "opened",
"created_at": "2018-02-19T13:46:38.751Z",
"updated_at": "2018-02-19T14:32:46.061Z",
"closed_at": null,
"labels": [],
"milestone": null,
"assignees": [],
"author": {
"id": 19,
"state": "active"
},
"assignee": null,
"user_notes_count": 0,
"upvotes": 0,
"downvotes": 0,
"due_date": null,
"confidential": false,
"discussion_locked": null,
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
}
}, {
"id": 1140,
"iid": 166,
"project_id": 16,
"title": "test2",
"description": "test2",
"state": "opened",
"created_at": "2018-02-19T11:01:00.729Z",
"updated_at": "2018-02-19T11:01:00.729Z",
"closed_at": null,
"labels": [],
"milestone": null,
"assignees": [{
"id": 5,
"state": "active"
}],
"author": {
"id": 5,
"state": "active"
},
"assignee": {
"id": 5,
"state": "active"
},
"user_notes_count": 0,
"upvotes": 0,
"downvotes": 0,
"due_date": null,
"confidential": false,
"discussion_locked": null,
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
}
}, {
"id": 1136,
"iid": 165,
"project_id": 17,
"title": "test3",
"description": "test3",
"state": "opened",
"created_at": "2018-02-16T15:36:22.712Z",
"updated_at": "2018-02-16T15:36:22.712Z",
"closed_at": null,
"labels": [],
"milestone": null,
"assignees": [{
"id": 5,
"state": "active"
}],
"author": {
"id": 1,
"state": "active"
},
"assignee": {
"id": 5,
"state": "active"
},
"user_notes_count": 0,
"upvotes": 0,
"downvotes": 0,
"due_date": null,
"confidential": false,
"discussion_locked": null,
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
}
}]
Now I want to remove each block with "project_id": 16. How can i remove every Block with "project_id": 16?
So i would like to remove this Block. And everyone else with "project_id": 16, if there were any more.
{
"id": 1140,
"iid": 166,
"project_id": 16,
"title": "test2",
"description": "test2",
"state": "opened",
"created_at": "2018-02-19T11:01:00.729Z",
"updated_at": "2018-02-19T11:01:00.729Z",
"closed_at": null,
"labels": [],
"milestone": null,
"assignees": [{
"id": 5,
"state": "active"
}],
"author": {
"id": 5,
"state": "active"
},
"assignee": {
"id": 5,
"state": "active"
},
"user_notes_count": 0,
"upvotes": 0,
"downvotes": 0,
"due_date": null,
"confidential": false,
"discussion_locked": null,
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
}
},
I'm grateful for any help.
Here a little snippet
$merge = array_filter($merge, function($item){ return $item['project_id'] != 16 })
I got a some trouble in my Laravel application, in Search function, when I do search the result is
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "4232343432",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
{
"id": 3,
"user_id": 16,
"identity_number": "213918273",
"name": "Pekerja_3",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2345234234",
"image": null,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"partner_id": null,
"skill": []
}
]
}
as you can see that "Skill" with id number 3 is empty, I want all in id number 3 is empty also.
My controller is :
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])->whereHas('skill');
And I want something like this :
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2534234234",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
]
}
The point is, if "skill" variable is empty then data not show and vice versa.
Thankyou any help will appreciate, sorry for bad english
You can add a callback to your whereHas function with the same filter as the with function
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])
->whereHas('skill', function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
})
->get();
You can try this $worker = Worker:has('skill')->get();
Only worker that have at least one skill are contained in the collection
I have a query that runs group by and order by with eager loading and it seems group by works fine but order by doesn't work.
Message::whereIn('conversation_id',$msgs)
->where('deleted', '!=', $userId)
->with(array('last_sender' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->with(array('last_reciever' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->orderBy('id', 'desc')
->orderBy('conversation_id', 'desc')
->groupBy('id')
->groupBy('conversation_id')
->get();
It returns data like this
{
"data": [
{
"id": 133,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message 2 to user second",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:43:14",
"updated_at": "2017-10-17 15:43:14"
},
{
"id": 132,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "second message to first user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:38:16",
"updated_at": "2017-10-17 15:38:16"
},
{
"id": 131,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message to second user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:37:49",
"updated_at": "2017-10-17 15:37:49"
},
{
"id": 130,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "Hi how are you",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:36:49",
"updated_at": "2017-10-17 15:36:49"
}
]
}
But I only want to return the first two latest results. Only id 133 and 132
I know I added ->groupBy('id') which is useless here and returning the all records. I added it just to show what my data are. So if I remove ->groupBy('id') then it only returns id 131 and 130
Please help. Thank you.
Ok I solve it adding one extra line :)
I am posting for other's helps.
->orderBy('created_at', 'desc')
->get()
->unique('conversation_id');
I am trying to use groupBy with Laravel's with function. Here is my query
$msgs=Conversation::where('sender', $sender)
->orWhere('reciever', $sender)
->orderBy('created_at', 'desc')
->with('message')
->get();
This query returns many records from the message table but I want to retrieve only one.
This is data and I want to group the result from the message object
{
"data": [
{
"id": 27,
"sender": 10,
"reciever": 4,
"created_at": "2017-07-26 22:46:04",
"updated_at": "2017-07-26 22:46:04",
"message": [
{
"id": 5,
"conversation_id": 27,
"last_sender": 10,
"msg": "5",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:04",
"updated_at": "2017-07-26 22:46:04"
},
{
"id": 6,
"conversation_id": 27,
"last_sender": 10,
"msg": "6",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:19",
"updated_at": "2017-07-26 22:46:19"
},
{
"id": 7,
"conversation_id": 27,
"last_sender": 10,
"msg": "7",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:35",
"updated_at": "2017-07-26 22:46:35"
},
{
"id": 8,
"conversation_id": 27,
"last_sender": 10,
"msg": "8",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:36",
"updated_at": "2017-07-26 22:46:36"
}
]
},
{
"id": 26,
"sender": 10,
"reciever": 7,
"created_at": "2017-07-26 22:40:02",
"updated_at": "2017-07-26 22:40:02",
"message": [
{
"id": 1,
"conversation_id": 26,
"last_sender": 10,
"msg": "1",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:40:02",
"updated_at": "2017-07-26 22:40:02"
},
{
"id": 2,
"conversation_id": 26,
"last_sender": 10,
"msg": "2",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:36",
"updated_at": "2017-07-26 22:45:36"
},
{
"id": 3,
"conversation_id": 26,
"last_sender": 10,
"msg": "3",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:50",
"updated_at": "2017-07-26 22:45:50"
},
{
"id": 4,
"conversation_id": 26,
"last_sender": 10,
"msg": "4",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:57",
"updated_at": "2017-07-26 22:45:57"
}
]
}
]
}
Any help is highly appreciated. Let me know if things are not clear. Thank you.
instead of 'with' replace it with this:
$msgs=Conversation::where('sender', $sender)
->orWhere('reciever', $sender)
->orderBy('created_at', 'desc')
->with(['message' => function ($query) { return $query->first();}])
->get();
this will return only one "message" instead of a collection of messages.
you can inside the callback function apply whatever whether it is (pluck, select,take...etc)