0

I have the following which I would like to order alphabetically by the Key i.e first for each array group would be "bname", followed by "created_at".

{
    "leads": [
        {
            "lead_id": 1,
            "zoho_lead": null,
            "bname": "ABC Limited",
            "tname": "ABC",
            "source_id": 11,
            "industry_id": 1,
            "user_id": 1,
            "created_at": "2017-09-06 15:54:21",
            "updated_at": "2017-09-06 15:54:21",
            "user": "Sean McCabe",
            "source": "Unknown",
            "industry": "None"
        },
        {
            "lead_id": 2,
            "zoho_lead": 51186111981,
            "bname": "Business Name Limited",
            "tname": "Trading Name",
            "source_id": 11,
            "industry_id": 1,
            "user_id": 1,
            "created_at": "2017-06-01 12:34:56",
            "updated_at": null,
            "user": "John Doe",
            "source": "Unknown",
            "industry": "None"
        }
    ]
}

I'm trying to use ksort like so in the foreach loop:

class LeadController extends Controller
{
    use Helpers;

    public function index(Lead $leads)
    {
        $leads = $leads->all();

        foreach($leads as $key => $lead){
            $lead->user = User::where('id', $lead->user_id)->first()->name;
            $lead->source = Source::where('id', $lead->source_id)->first()->name;
            $lead->industry = Industry::where('id', $lead->industry_id)->first()->name;

            $lead->ksort();
        }

        return $leads;
    }

But I get the following error:

Call to undefined method Illuminate\\Database\\Query\\Builder::ksort()

How do I use this function, or is there a Laravel way of doing this, or a better way altogether?

Thanks.

8
  • 1) By what "Key"? 2) Where is this data coming from? What is $lead? How is it assigned a value? Commented Sep 11, 2017 at 1:15
  • @Phil see edit with clarifications Commented Sep 11, 2017 at 1:21
  • The order of object keys should not make any difference to your data structure. Why does it matter? Commented Sep 11, 2017 at 1:23
  • Personally I don't care what order they come out in, as I can call each by name, but I have been asked to have them output in alphabetical order. Commented Sep 11, 2017 at 1:25
  • Obviously $leads->all() is returning a traversable collection of Illuminate\Database\Query\Builder objects whereas you seem to expect them to be something else with a ksort method. You should probably work out why that is the case Commented Sep 11, 2017 at 1:29

2 Answers 2

2

Managed to get it to return with the Keys in alphabetical order, so below is the solution in-case someone else should require it:

public function index(Lead $leads)
{
    $leadOut = Array();

    $leads = $leads->all();

    foreach($leads as $key => $lead){
        $lead->user = User::where('id', $lead->user_id)->first()->name;
        $lead->source = Source::where('id', $lead->source_id)->first()->name;
        $lead->industry = Industry::where('id', $lead->industry_id)->first()->name;

        //Convert to Array
        $leadOrder = $lead->toArray();
        //Sort as desired
        ksort($leadOrder);
        //Add to array
        $leadOut[] = $leadOrder;
    }

    return $leadOut;
}

There is likely a cleaner way to do this, but it works for my instance, and perhaps additional answers may be posted that are better.

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

Comments

0

You could do something like:

return Lead::with('user', 'source', 'industry')->get()->map(function ($lead) {

        $item = $lead->toArray();

        $item['user'] = $lead->user->name;
        $item['source'] = $lead->source->name;
        $item['industry'] = $lead->industry->name;

        ksort($item);

        return $item;
    });

This should be much more efficient as it will eager load the relationships rather than make 3 extra queries for each iteration.

3 Comments

I get the error: "Illegal offset type in isset or empty"
@SeánMcCabe What version of PHP are you using and what version of Laravel are you using?
5.6.31 and 5.4. I've managed to solve the whole issue now using Fractal, allows me to output the stuff in the order that has been requested and change the names for security. Controller now just has the two lines $leads = Lead::with('user', 'source', 'industry', 'status')->get(); return fractal($leads, new LeadTransformer())->respond();

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.