4

here is my Query

$RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->pluck('employee_name','email');

which give me the proper result as i want,

but after i execute query i have 1 more key => value pair to push in the result array.

If i print the current result , its something like this .

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [punit@*****.com] => Punit Gajjar
            [milan@*****.com] => Milan Gajjar
            [pritesh@*****.com] => Pritesh Modi
            [pratik@*****.com] => Pratik Modi
            [jyoti@*****.com] => Jyotiranjan J..
        )

)

Bit if i try to push my Key=>valye pair into this array it dosn't work.

array_push(array("All"=>"All"),$RecipientList);

Need Output something like

Illuminate\Support\Collection Object
    (
        [items:protected] => Array
            (
                [All] => All
                [milan@*****.com] => Milan Gajjar
                [milan@*****.com] => Milan Gajjar
                [pritesh@*****.com] => Pritesh Modi
                [pratik@*****.com] => Pratik Modi
                [jyoti@*****.com] => Jyotiranjan J..
            )

    )
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70

2 Answers2

6

It is because $RecipientList is Collection and not the Array.

Try this

RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->pluck('employee_name','email')->toArray();

If this does not work, try below code

RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->get()->pluck('employee_name','email')->toArray();

Hope this will help you.

kapilpatwa93
  • 4,111
  • 2
  • 13
  • 22
4

You have Illuminate\Support\Collection object not array. You can do

$RecipientList->push(["All"=>"All"]);

UPD: There is prependmethod

$RecipientList->prepend('All', 'All');
aleksejjj
  • 1,715
  • 10
  • 21
  • Nope.. My result is `Illuminate\Support\Collection Object ( [items:protected] => Array ( [punit@*****.com] => Punit Gajjar [milan@*****.com] => Milan Gajjar [pritesh@*****.com] => Pritesh Modi [pratik@*****.com] => Pratik Modi [jyoti@*****.com] => Jyotiranjan J.. [0] => Array ( [All] => All ) ) )` after using your solution. – Punit Gajjar Oct 25 '16 at 08:14
  • 1
    @PunitGajjar How about `prepend` method? `$RecipientList->prepend('All', 'All');` – aleksejjj Oct 25 '16 at 08:44