0

I get a list of Project Objects by doing this

$projects = Project::orderBy('deploymentDate', 'ASC')->get();

The output of the above is something like this

Collection {#292
  #items: array:2 [
    0 => Project {#293
      #attributes: array:15 [
        "id" => "4"
        "name" => "Something"
        "value" => "234"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "sdfsd"
      ]
    }
    1 => Project {#294
      #attributes: array:15 [
        "id" => "3"
        "name" => "Something"
        "value" => "6"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "John Doe"
      ]
    }
  ]
}

I have removed a lot of the unnecessary data. So you can see I have two projects. The problem is, these projects have relationships with other models, so I need to create an array containing all of this data. So if I do

foreach($projects as $project) {
    dd($project->projectType);
}

I can see this

#attributes: array:6 [
    "id" => "2"
    "additionalInformation" => null
    "project_id" => "4"
]

So I have created an array, and I plan on adding the project along with its relationships to this array. At the moment I am trying this

$projectsArray = array();
foreach($projects as $project) {
    $projectsArray[] = array(
        "Project" => json_decode($project, true)
    );

    if($project->projectType) {
        $projectsArray[] = array(
            "Types" => json_decode($project->projectType, true)
        );
    }
}

The issue with this is that it outputs the following

array:4 [
  0 => array:1 [
    "Project" => array:15 [
       "id" => "4"
        "name" => "Something"
        "value" => "234"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "sdfsd"
    ]
  ]
  1 => array:1 [
    "Types" => array:6 [
       "id" => 2
      "additionalInformation" => null
      "project_id" => "4"
    ]
  ]
  2 => array:1 [
    "Project" => array:15 [
     "id" => "3"
        "name" => "Something"
        "value" => "6"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "John Doe"
    ]
  ]
  3 => array:1 [
    "Types" => array:6 [
      "id" => 1
      "additionalInformation" => null
      "project_id" => "3"
    ]
  ]
]

So it is adding the data to the array, but not nesting it as relations. Ideally, I would be after something like this for each project

array:1 [
  "Project" => array:2 [
    "Project" => array:15 [
      "id" => 3
      "name" => "Something"
      "value" => "6"
      "user_id" => "1"
      "client_id" => "97"
      "contact" => "John Doe"
    ]
    0 => array:1 [
      "Types" => array:6 [
        "id" => 1
        "additionalInformation" => null
        "project_id" => "3"
      ]
    ]
  ]
  "Project" => ...
]

How could this be achieved?

Thanks

1
  • 3
    dd is not a builtin. Neither is Collection. This looks like laravel - is it specific to the framework? Please either change the code to show how you reproduced the problem with generic php functions, or add a tag for the specific framework you are using. Commented Aug 18, 2016 at 14:12

3 Answers 3

1

Your problem is with how you are building the array. Youa re doing this:

$projectsArray = array();
foreach($projects as $project) {
// Add [Project => []] to $projectsArray
    $projectsArray[] = array(
        "Project" => json_decode($project, true)
    );
// $projectsArray now has one element, which is an array

    if($project->projectType) {
// Add [Types => []] to $projectsArray
        $projectsArray[] = array(
            "Types" => json_decode($project->projectType, true)
        );
// $projectsArray now has two array elements, each with a single key
    }
}

It sounds like what you want, is to add the Types key/value pair to the array produced by your json_decode, in which case you need to identify that as a distinct thing you can add to, like this:

$projectsArray = array();
foreach($projects as $project) {
    $projectarr = array(
        "Project" => json_decode($project, true)
    );

    if($project->projectType) {
        $projectarr["Types"] = json_decode($project->projectType, true);
    }
    $projectsArray[] = $projectarr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I see one problem though, I will no longer be able to do $project->projectType because at this point $project is an Array, not an Object. Should I call the array something else?
@kate_hudson yes, I've edited the answer now, thanks for pointing that out
1

Just create a $project var and array_push it in your array when it's done (primary datas then related datas added).

$newProject = array();
$newProject["Project"] = json_decode($project, true);
if($project->projectType) {
    $newProject["Types"] = json_decode($project->projectType, true);
}

array_push($projectsArray, $newProject);
// of $projectArray[] = $newProject;

Comments

-1

Why don't you try something like this:

$projectsArray = array();
foreach($projects as $project) {
    $projectTmp = json_decode($project, true);
    if($project->projectType) {
        $projectTmp["Types"] = json_decode($project->projectType, true);
    }
    $projectsArray[] = array("Project" => $projectTmp);
}

1 Comment

Could someone explain why this was down voted, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.