0

I am working on a laravel application at the moment, and I am trying to create an array from an object that is returned from the database by eloquent. The object looks like this,

        [
      {
        "id": 3,
        "body": "Fake User created the project Fake Project 2",
        "uri": "8jss90xk",
        "object_id": 422,
        "user": [
          {
            "id": 25,
            "email": "[email protected]",
            "first_name": "xxx1",
            "last_name": "xxxx1",
            "display_name": "xxxxxxxxxxx",
            "initials": "XX",
            "remember_me": null,
            "active": "1",
            "invite_code": null,
            "forgotten_code": null,
            "cost_visible": 0,
            "login_type": "normal",
            "api_token": null,
            "created_at": "2015-02-13 11:47:24",
            "updated_at": "2015-05-06 22:43:23",
            "deleted_at": null,
            "pivot": {
              "notification_id": 3,
              "user_id": 25,
              "is_read": 0
            }
          }
        ],
        "project": {
          "id": 422,
          "name": "Project Number 1",
          "slug": "Tdc3de97",
          "uri_hash": "project-number-1",
          "description": null,
          "total_cost": "1000.00",
          "start_date": "2015-07-21",
          "finish_date": "2015-10-22",
          "status": 2,
          "sales_person": null,
          "client_id": 0,
          "organisation_id": 97,
          "owner_id": 97,
          "user_id": 25,
          "locked_by": null,
          "created_at": "2015-07-21 13:39:21",
          "updated_at": "2015-07-24 10:07:38",
          "deleted_at": null,
          "archived_at": "0000-00-00 00:00:00",
          "invoiced_at": null,
          "is_internal": 0
        }
      },
      {
        "id": 4,
        "body": "Fake User created the project Fake Project 3",
        "uri": "8jss90xk",
        "object_id": 422,
        "user": [
          {
            "id": 25,
            "email": "[email protected]",
            "first_name": "XXXXX",
            "last_name": "XXXXXXXX",
            "display_name": "XXX",
            "initials": "XX",
            "remember_me": null,
            "active": "1",
            "invite_code": null,
            "forgotten_code": null,
            "cost_visible": 0,
            "login_type": "normal",
            "api_token": null,
            "created_at": "2015-02-13 11:47:24",
            "updated_at": "2015-05-06 22:43:23",
            "deleted_at": null,
            "pivot": {
              "notification_id": 4,
              "user_id": 25,
              "is_read": 0
            }
          }
        ],
        "project": {
          "id": 422,
          "name": "Project Number 1",
          "slug": "Tdc3de97",
          "uri_hash": "project-number-1",
          "description": null,
          "total_cost": "1000.00",
          "start_date": "2015-07-21",
          "finish_date": "2015-10-22",
          "status": 2,
          "sales_person": null,
          "client_id": 0,
          "organisation_id": 97,
          "owner_id": 97,
          "user_id": 25,
          "locked_by": null,
          "created_at": "2015-07-21 13:39:21",
          "updated_at": "2015-07-24 10:07:38",
          "deleted_at": null,
          "archived_at": "0000-00-00 00:00:00",
          "invoiced_at": null,
          "is_internal": 0
        }
      },
      {
        "id": 5,
        "body": "Fake User created the project Fake Project 4",
        "uri": "8jss90xk",
        "object_id": 422,
        "user": [
          {
            "id": 25,
            "email": "[email protected]",
            "first_name": "XXXXXX",
            "last_name": "XXXXXXX",
            "display_name": "XXXXXXX",
            "initials": "XX",
            "remember_me": null,
            "active": "1",
            "invite_code": null,
            "forgotten_code": null,
            "cost_visible": 0,
            "login_type": "normal",
            "api_token": null,
            "created_at": "2015-02-13 11:47:24",
            "updated_at": "2015-05-06 22:43:23",
            "deleted_at": null,
            "pivot": {
              "notification_id": 5,
              "user_id": 25,
              "is_read": 0
            }
          }
        ],
        "project": {
          "id": 422,
          "name": "Project Number 1",
          "slug": "Tdc3de97",
          "uri_hash": "project-number-1",
          "description": null,
          "total_cost": "1000.00",
          "start_date": "2015-07-21",
          "finish_date": "2015-10-22",
          "status": 2,
          "sales_person": null,
          "client_id": 0,
          "organisation_id": 97,
          "owner_id": 97,
          "user_id": 25,
          "locked_by": null,
          "created_at": "2015-07-21 13:39:21",
          "updated_at": "2015-07-24 10:07:38",
          "deleted_at": null,
          "archived_at": "0000-00-00 00:00:00",
          "invoiced_at": null,
          "is_internal": 0
        }
      }
}]

Basically the query returns all the users with unread notifications, and I want to build that into array along with any notifications and send an email but the array needs to be quite specific with the details it contains.

I imagine sending something like this to mail function that will send the reminders,

'[email protected]' => array(
   'first_name' => 'User',
   'last_name'  => 'Lastname',
   'projects' => array(
       'Project Title 1' => array(
         'notifications' => array(
            [0] => 'Notification 1',
            [1] => 'Notification 2',
            [2] => 'Notification 3'
         )
       )
   )
)

I cannot for the live of me work out how to build this array from my object, I currently have this in my code,

foreach($unread as $object) {
    $emails_to_send = array(
        'user' => $object->user[0],
        'uri' => $object->uri,
        'body' => $object->body,
        'project' => $object->project
    );
}

This however builds the wrong structure and overwrites the last loop.

2 Answers 2

1

The reason why you get the data overwritten in the last loop is because you assing newly created array to $emails_to_send variable instead of appending to the array. Try the following:

$emails_to_send = array();

foreach($unread as $object) {
  $emails_to_send[] = array(
    'user' => $object->user[0],
    'uri' => $object->uri,
    'body' => $object->body,
    'project' => $object->project
  );
}

var_dump($emails_to_send);

This should give you a list containing one entry for each email that needs to be sent.

Sign up to request clarification or add additional context in comments.

Comments

0

You are wright you have to iterate through your resulting array but like this:

// First create the final array
$emails_to_send = array();
foreach($unread as $object) {
    // Add new array element with the email key
    $emails_to_send[$object->user[0]->email] = array(
        'first_name' => $object->user[0]->first_name,
        'last_name' => $object->user[0]->last_name,
        'project' => $object->project
    );
}

var_export($emails_to_send);

As a result you will see something like:

array(
    '[email protected]' => array(
        'first_name' => 'User',
        'last_name'  => 'Lastname',
        'project' => array(
            "id" => 422,
            "name" => "Project Number 1",
            "slug" => "Tdc3de97",
            "uri_hash" => "project-number-1",
            ...
        )
    ),
    ...
)

Comments

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.