1

i have a query like that:

$pd= DB::table("projects")
            ->orderBy("project_number", "asc")
            ->select('name' , "type")
            ->get()->keyBy('date_scheduled');;

and i get a array keyed By the date_scheduled which is fine, but the problem is date_scheduled is not unique so it overwrites the entries. So i need to get a two dimensional array with keyby date_scheduled like:

[
 'date_1_of_date_scheduled' => [0 => '1', 1 => '4', 2 => '17']
 'date_2_of_date_scheduled' => [0 => '15', 1 => '64', 2 => '1142']
 'date_3_of_date_scheduled' => [0 => '25', 1 => '125', 2 => '66']
]

2 Answers 2

1

You can use groupBy() instead of keyBy(): https://laravel.com/docs/8.x/collections#method-groupby

$pd = DB::table("projects")
    ->orderBy("project_number", "asc")
    ->select('name' , "type")
    ->get()
    ->groupBy('date_scheduled');
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the laravel map function for the purpose

Try that:

$pd= DB::table("projects")
            ->orderBy("project_number", "asc")
            ->select('name' , "type")
            ->get()
            ->map(function ($item) {
               return ['date_'.$item->date_scheduled.'_of_date_scheduled' => $item->YOUR_DESIRED_DATA] ;
             }

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.