PHP Hupp Technologies
PHP Hupp Technologies

Reputation: 1952

Modifiy response in laravel

Below my Laravel controller code

public function templateValueData($template_id)
{
    $parent = TemplateMetadataValue::where('template_id', $template_id)->get();

    return $parent;
}

Here, the output of the method above;

[
  {
    "id": 11,
    "parent_id": 0,
    "name": "Category 1"
  },
  {
    "id": 63,
    "parent_id": 11,
    "name": "Category 2"
  },
  {
    "id": 73,
    "parent_id": 63,
    "name": "Category 3"
  },
  {
    "id": 77,
    "parent_id": 11,
    "name": "Category 5"
  },
  {
    "id": 83,
    "parent_id": 77,
    "name": "Category 8"
  }
]

Now I want to format this response base on the value of parent_id.

The condition is: when parent_id is equal to id, then the parent_id is considered as children of the id.

Please note that both parent_id and id are stored as incremental values.

I want to alter and return the response as below;

[
  {
    "id": 11,
    "parent_id": 0,
    "name": "Category 1",
    "children": [
      {
        "id": 63,
        "parent_id": 11,
        "name": "Category 2",
        "children": [
          {
            "id": 73,
            "parent_id": 63,
            "name": "Category 3"
          }
        ]
      },
      {
        "id": 77,
        "parent_id": 11,
        "name": "Category 5",
        "children": [
          {
            "id": 83,
            "parent_id": 77,
            "name": "Category 8"
          }
        ]
      }
    ]
  }
]

How can I achieve this?

Upvotes: 3

Views: 97

Answers (3)

PHP_only
PHP_only

Reputation: 75

Use below code for achieve your response.

public function templateValueData($template_id)
{
    $parents = TemplateMetadataValue::where('template_id', $template_id)->get();
    $arr = $this->xyz($parents);
    return $arr;
} 

Function for achieve response.

function xyz($parents,$parent_id = 0){
    $res = [];
    foreach ( $parents as $parent ) {
        if ( $parent[ "parent_id"] == $parent_id ){
            $children = $this->xyz($parents, $parent[ "id"]);
            if ($children) {
                 $parent[ "children"] = $children;
            } 
            $res[]=$parent;
        }
    }
    return $res;
}

Upvotes: 1

jogesh_pi
jogesh_pi

Reputation: 9782

If you slightly modify your TemplateMetadataValue model then you could achieve the desired result.

class TemplateMetadataValue extends Model {

    public function children() {
        return $this->hasMany(static::class, 'parent_id');
    }
}

Call it with children method.

$parent = TemplateMetadataValue::with('children')->get();

Rest of the login you can apply here with your query builder.

Upvotes: 0

train_fox
train_fox

Reputation: 1537

You should call your relation on your controller:

public function templateValueData($template_id)
{
    $parent = TemplateMetadataValue::where('template_id',$template_id)->with('children')->get();

    return $parent;
}

Also you can use method injection on your controller methods, so you can use this code:

public function templateValueData(TemplateMetadataValue $template)
{
    return $template->load('children')->get();
}

Upvotes: 0

Related Questions