0

I recently started learning Laravel and I'm trying to implement the api of whmcs for a project. I want to loop trough an array, get the id of multiple support tickets (conversations) and pass those id's to a function that calls my back end to retrieve those tickets.

The following function returns all the tickets a user has. It contains the Ticket ID (tid) and with the following function a second API call must be made to retrieve the ticket's content.

public function getTickets($userid)
    {
        $tickets = Whmcs::execute('gettickets',
            array(
                'clientid'  => $userid,
            )
        );

        return $tickets['tickets'];
    }

$tickets['ticket'] contains this array:

{"ticket":
    [{"
      id":"2",
      "tid":"363592",
      "deptid":"1",
      "userid":"1",
      "name":"Ilyas Deckers",
      "email":"[email protected]",
      "cc":"","c":"lOXHYuKV",
      "date":"2016-06-02 13:50:16",
      "subject":"Another ticket",
      "status":"Open",
      "priority":"Medium",
      "admin":"","attachment":"","lastreply":"2016-06-02 13:50:16","flag":"0","service":""},

    { "id":"1",
      "tid":"701236",
      "deptid":"1",
      "userid":"1",
      "name":"Ilyas Deckers",
      "email":"[email protected]","cc":"","c":"76Rl9wKG",
      "date":"2016-06-02 12:28:47","subject":"Theory of design","status":"Customer-Reply","priority":"Medium","admin":"","attachment":"","lastreply":"2016-06-02 12:30:03","flag":"0","service":""}]} 

This array has 2 tickets, id 1 and id 2. If I want to know what the content is of those tickets I can get that data with the following:

function getTicket($id)
        {
            $ticket = Whmcs::execute('getticket',
                array(
                    'ticketid'  => $id,
                )
            );

            return $ticket;
        }

This returns this array when I pass one id:

{
  "result":"success",
  "ticketid":"1",
  "tid":"701236",
  "name":"Ilyas Deckers",

  ...

  "priority":"Medium",
  "admin":"",
  "lastreply":"2016-06-02 12:30:03",
  "flag":"0","service":"",
  "replies":{
    "reply":[{
      "userid":"1",
      "contactid":"0",
      "name":"Ilyas Deckers",
      "email":"[email protected]",
      "date":"2016-06-02 12:28:47",
      "message":"Hi Steve,\r\n\r\nVisual hierarchy is one of the most important principles behind effective web design. We will see how you can use some very basic exercises in your own designs to put these principles into practice.\r\n\r\nDesign is a funny word. Some people think design means how it looks. But of course, if you dig deeper, it's really how it works.\r\nAt it's core, design is all about visual communication: To be an effective designer, you have to be able to clearly communicate your ideas to viewers or else lose their attention.\n\n----------------------------\nIP Address: 127.0.0.1",
      "attachment":"",
      "admin":""},
    { 
      "userid":"1",
      "contactid":"0",
      "name":"Ilyas Deckers",
      "email":"[email protected]",
      "date":"2016-06-02 12:30:03",
      "message":"Hi Steve,\r\n\r\nVisual hierarchy is one of the most important principles behind effective web design. We will see how you can use some very basic exercises in your own designs to put these principles into practice.\r\n\r\nDesign is a funny word. Some people think design means how it looks. But of course, if you dig deeper, it's really how it works.\r\nAt it's core, design is all about visual communication: To be an effective designer, you have to be able to clearly communicate your ideas to viewers or else lose their attention.",
      "attachment":"863372_Clientv2.PNG",
      "admin":"",
      "rating":"0"
    }]},
  "notes":""}

How can I pass $id=1 and $id=2 trough the same function and combine those results in one array to use in a foreach loop in my template? I was thinking to loop trough the first array, get the id and pass it trough function getTicket($id). This works, but only gives one result.

    $tickets = $this->getTickets($uid);                
    foreach ($tickets['ticket'] as $getid) 
    {
        $id = $getid['id'];
        $this->getTicket($id);

        return $ticket;
    }

2 Answers 2

3

You are returning something inside your foreach() loop. This means it will stop in the first loop.

You could do something like:

$tickets = $this->getTickets($uid);

$tickets_content = [];

foreach ($tickets['ticket'] as $getid)
{

    $id = $getid['id'];

    $tickets_content[] = $this->getTicket($id);

}

return $tickets_content;
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe using collections

return collect($this->getTickets($uid))->map(function($ticket){

    return [$ticket, $this->getTicket($ticket['id'])];

})->flatten(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.