Remove JSON Block with PHP - php

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 })

Related

laravel relationship tickets, ticket messages and message attachments

I am writing a support ticket system in laravel. I need to establish relationships between tables. I'm new to Laravel. I need to get the messages of a ticket and the files of those messages. By reading the documentation, I have established a relationship between the messages belonging to the ticket and it works correctly. However, I need to get the files under those messages, can you help me with this?
The json of the messages is as follows:
{
"ticket": [
{
"id": 1,
"subject": "test 1",
"department_id": 1,
"priority": "low",
"status": "open",
"users_id": 1,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-04T17:31:10.000000Z",
"deleted_at": null,
"author": "Admin Admin",
"author_id": 1,
"department": "Genel",
"messages": [
{
"id": 1,
"admin_tickets_id": 1,
"users_id": 1,
"admin_users_id": null,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
},
{
"id": 5,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
},
{
"id": 9,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test için yaziyom",
"deleted_at": null,
"created_at": "2022-10-04T22:19:32.000000Z",
"updated_at": "2022-10-04T22:19:32.000000Z"
},
{
"id": 10,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:26:39.000000Z",
"updated_at": "2022-10-04T22:26:39.000000Z"
},
{
"id": 11,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:27:00.000000Z",
"updated_at": "2022-10-04T22:27:00.000000Z"
},
{
"id": 12,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:28:45.000000Z",
"updated_at": "2022-10-04T22:28:45.000000Z"
},
{
"id": 13,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "yine deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:32:34.000000Z",
"updated_at": "2022-10-04T22:32:34.000000Z"
}
]
}
]
}
but I want it to be like this:
Pay attention to the file below the message.
{
"ticket": [
{
"id": 1,
"subject": "test 1",
"department_id": 1,
"priority": "low",
"status": "open",
"users_id": 1,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-04T17:31:10.000000Z",
"deleted_at": null,
"author": "Admin Admin",
"author_id": 1,
"department": "Genel",
"messages": [
{
"id": 1,
"admin_tickets_id": 1,
"users_id": 1,
"admin_users_id": null,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
"attachments": [
{
"id": 1
"name": "deneme",
"extension": "pdf",
},
]
},
{
"id": 5,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
},
{
"id": 9,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test için yaziyom",
"deleted_at": null,
"created_at": "2022-10-04T22:19:32.000000Z",
"updated_at": "2022-10-04T22:19:32.000000Z"
},
{
"id": 10,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:26:39.000000Z",
"updated_at": "2022-10-04T22:26:39.000000Z"
},
{
"id": 11,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:27:00.000000Z",
"updated_at": "2022-10-04T22:27:00.000000Z"
},
{
"id": 12,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:28:45.000000Z",
"updated_at": "2022-10-04T22:28:45.000000Z"
},
{
"id": 13,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "yine deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:32:34.000000Z",
"updated_at": "2022-10-04T22:32:34.000000Z"
}
]
}
]
}
I share my controllers and models:
TicketsController.php
public function show($id) {;
$data = array();
$data['ticket'] = AdminTickets::addSelect([
'author' => Users::select(DB::raw("name || ' ' || surname as fullname"))
->whereColumn('id', 'admin_tickets.users_id')
->limit(1),
'author_id' => Users::select('id')
->whereColumn('id', 'admin_tickets.users_id')
->limit(1),
'department' => AdminTicketDepartments::select('name')
->whereColumn('id', 'admin_tickets.department_id')
->limit(1),
])->where('id', $id)->with('messages', fn ($query) =>
$query->whereHas('attachments')
)->get();
return $data;
die();
return view('admin.tickets.show', $data);
}
AdminTickets Model:
public function messages()
{
return $this->hasMany(AdminTicketReplies::class);
}
AdminTicketReplies Model:
public function attachments() {
return $this->hasManyThrough(AdminTicketAttachments::class, AdminTicketReplies::class);
}
I think I'm making a mistake in the AdminTicketReplies model. How can I do that?
You are now getting all messages that have at least one attachment. you can add the attachments by using another with():
->where('id', $id)->with('messages', fn ($query) =>
$query->whereHas('attachments')
$query->with('attachments')
)->get();
If you want all messages and include the attachments, you can use the dot notation:
->where('id', $id)->with('messages.attachments')

Obtain KEY Value Array in variable PHP

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.

How to modify array structure returned by eloquent in laravel

I currently have the below array structure that I get when running Bonus::with('users')->get()
I have tried adding the array and merging similar keys but it is not working. I tried using the helper methods provided in laravel but I don't seem to be able to get it working.
[
{
"id": 1,
"users_id": 1,
"bonus_type_id": 1,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 2,
"users_id": 1,
"bonus_type_id": 2,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 3,
"users_id": 2,
"bonus_type_id": 2,
"is_paid": 1,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "another test",
"email": "another#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
}
]
I want the result to be something like that
[
{
user_id: 1,
name: "test",
bonuses: [
{
bonus_type_id: 1,
amount: 100,
is_paid: 0,
},
{
bonus_type_id: 2,
amount: 100,
is_paid: 0,
}
],
},
{
user_id: 2,
name: "another",
bonuses: [
{
bonus_type_id: 2,
amount: 100,
is_paid: 1,
},
]
}
]
You need to change your eloquent relationship. Define hasMany relationship function bonuses inside User model for bonuses table and use that to get the desired result like so:
User::with('bonuses')->get();
That way it will return results from users table and populate bonuses for each user.
makes sense?
You can get specific information with select subquery, if your relations are correctly set, this should work.
User::select('user_id','name','bonus_id')
->with(array('bonuses'=> function($query){
$query->select('bonus_type_id','amount','is_paid');
}))->get();

How to remove index number from laravel eloquent unique

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);

Is it possible to use groupBy with a "with" function in laravel eloquent

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)

Categories