PHP Group Json Objects - php

"forms": [
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 21,
"field_caption": "Gön.Hekim"
}
},
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 22,
"field_caption": "Kurum"
}
},
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 23,
"field_caption": "Endikasyon"
}
},
{
"id": 2,
"name": "edit_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 2,
"field_name_id": 24,
"field_caption": "Materyal"
}
}
]
I want to group forms object in same name/id.
How i want...
"forms": [
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"fields": [{
"field_name_id": 21,
"field_caption": "Gön.Hekim"
},{
"field_name_id": 22,
"field_caption": "Kurum"
},{
"field_name_id": 23,
"field_caption": "Endikasyon"
}]
},
{
"id": 2,
"name": "edit_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"fields": [{
"field_name_id": 24,
"field_caption": "Materyal"
}]
}
]
i searched in so and googled, but i could not find any method for my problem. There are answers to similar questions.
i did with foreach loop but i thought may be there is a method for this in php.
And also, if i have over an hundred objects in json, foreach can be slow.
Thanks in advice.

Try the following:
$arr = json_decode($str, true);
$arr2 = [];
$ids = [];
foreach($arr['forms'] as $form)
{
if(!in_array($form['id'], $ids))
{
$ids[] = $form['id'];
$arr2[$form['id']] = $form;
}
unset($arr2[$form['id']]['pivot']);
$arr2[$form['id']]['fields'][] = [$form['pivot']['field_name_id'], $form['pivot']['field_caption']];
}
$str2 = json_encode(['forms' => array_values($arr2)]);
echo $str2;
This uses a foreach loop to get unique forms and will create a new key fields to hold the field_name_id and field_caption and also removes the pivot key

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

How can loop & get values of unlimited parent-child & sub-child tree data in laravel

I have data like parent-child with unlimited levels of sub-child & sub-child data in the database which you can see below like a binary tree with nodes that have multiple children and these children can have unlimited sub-child.
Please give me a solution or suggestion how can show that. Thanks.
This is my database JSON data which I'm getting from the database...
Controller Code
$products = Product::where('project_id', $project_id )->whereNull('parent_id')->with('categories')->with('childProducts')->get();
Getting data from the above object this data want to save in the other way you can see below
{
"data": {
"products": [{
"id": 1,
"name": "ParentProduct",
"project_id": 1,
"parent_id": null,
"created_by": 1,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 6,
"product_id": 1,
"project_id": 1,
"name": "TopLevelCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 2,
"name": "LevelOneProduct",
"project_id": null,
"parent_id": 1,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 1,
"product_id": 2,
"project_id": 1,
"name": "LevelOneProductCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 3,
"name": "LevelOneProductChildFolder",
"project_id": null,
"parent_id": 2,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 2,
"product_id": 3,
"project_id": 1,
"name": "evelOneProductChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 4,
"name": "LevelOneProductSubChildFolder",
"project_id": null,
"parent_id": 3,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 3,
"product_id": 4,
"project_id": 1,
"name": "LevelOneProductSubChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": []
}]
}]
},
{
"id": 5,
"name": "LeveTwoProduct",
"project_id": null,
"parent_id": 1,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [],
"child_products": [{
"id": 6,
"name": "LeveTwoProductFolder",
"project_id": null,
"parent_id": 5,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 4,
"product_id": 6,
"project_id": 1,
"name": "LeveTwoProductFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 7,
"name": "LeveTwoProductChildFolder",
"project_id": null,
"parent_id": 6,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 5,
"product_id": 7,
"project_id": 1,
"name": "LeveTwoProductChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": []
}]
}]
}
]
}]
}
}
I want to take this data to another JSON data which I have to give you an example as mentioned below I want to loop the above JSON data and want to make their need JSON with this data as like Dear Problem is that How can loop above data and can give to the other JSON keys you can see below. I want all data with loop and want to get all values and want equal all values to the keys as below
foreach ($products as $product) {
foreach ($product->categories as $singleCaegory) {
$singleCaegoryData = [
"name" => $singleCaegory->name,
"request" => [
'method' => $singleCaegory->type,
'body' => [
"mode" => "raw",
'raw' => $singleCaegory->raw_data
],
'url' => [
"raw" => $singleCaegory->url
],
],
];
}
}
I think you understand my problem. that I want to loop above data which I have already in the database and fetching from that. I only want to loop all data and equal to the needed keys. Thanks.
for saving Id's you only need:-parent Id, child_Id_count(count of the only direct child), total_child_count(count all children below).that's it by using these data and created date you can generate a tree view.

Laravel Eloquent perform Aggregation function in the pivot relationship

I would like to perform the average rating of respective product which stored in the pivot table called "reviews".Below are my existing code.
Product Model:
public function reviews(){
return $this->belongsToMany(User::class,'reviews')->withPivot('comment','rating')->withTimestamps();
}
public function getProductRatingAttribute(){
return $this->reviews()->average('rating');
}
User Model:
public function reviews(){
return $this->belongsToMany(Product::class,'reviews')->withPivot('comment','rating')->withTimestamps();
}
Migration for the reviews
Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('product_id');
$table->string('comment')->nullable();
$table->integer('rating');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
I have attempted to use the below to code construct the expected output but it leads N+1 issue because it does not take advantage of the eager loading of the Laravel Eloquent.
$s = Product::with('reviews')->append('product_rating');
As I check telescope, it produces 6 query which can lead performance issue when the data is large.
Expected output:
{
"msg": [
{
"id": 4,
"created_at": "2021-04-09T07:32:35.000000Z",
"updated_at": "2021-04-09T07:32:35.000000Z",
"name": "MacDonald",
"nickname": "MCD",
"users_id": 1,
"price": "0.00",
"product_rating": "3.3333",
"reviews": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 4,
"user_id": 1,
"comment": "Ouch",
"rating": 3,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"pivot": {
"product_id": 4,
"user_id": 2,
"comment": "Ouch",
"rating": 5,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
},
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 4,
"user_id": 1,
"comment": "Ouch",
"rating": 2,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
}
]
},
{
"id": 10,
"created_at": null,
"updated_at": null,
"name": "Mary Bown",
"nickname": "MB",
"users_id": 1,
"price": "2.88",
"product_rating": "2.0000",
"reviews": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 10,
"user_id": 1,
"comment": "Ouch",
"rating": 2,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
}
]
},
{
"id": 11,
"created_at": null,
"updated_at": null,
"name": "Pizza Hut",
"nickname": "pizzahut",
"users_id": 1,
"price": "4.10",
"product_rating": null,
"reviews": [
]
},
{
"id": 12,
"created_at": "2021-04-09T08:00:42.000000Z",
"updated_at": "2021-04-09T08:00:42.000000Z",
"name": "Domino Pizza",
"nickname": "domino",
"users_id": 3,
"price": "0.00",
"product_rating": null,
"reviews": [
]
},
{
"id": 13,
"created_at": "2021-04-26T16:12:53.000000Z",
"updated_at": "2021-04-26T16:12:53.000000Z",
"name": "Chicken Chop",
"nickname": "chickchop",
"users_id": 3,
"price": "0.00",
"product_rating": null,
"reviews": [
]
}
]
}
Appending is performing the query on each record.
Why not perform average on the collection itself?
$s = Product::with('reviews')->get();
$avg = $s->avg('pivot.rating');
Alternatively you can modify the relationship method to include a raw select of the average rating.

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 can I hidden data in JSON if variable is empty

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

Categories