Php : Laravel - Array push after using eloquent pluck method

299

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
        (
            [[email protected]*****.com] => Punit Gajjar
            [[email protected]*****.com] => Milan Gajjar
            [[email protected]*****.com] => Pritesh Modi
            [[email protected]*****.com] => Pratik Modi
            [[email protected]*****.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
                [[email protected]*****.com] => Milan Gajjar
                [[email protected]*****.com] => Milan Gajjar
                [[email protected]*****.com] => Pritesh Modi
                [[email protected]*****.com] => Pratik Modi
                [[email protected]*****.com] => Jyotiranjan J..
            )

    )
211

Answer

Solution:

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.

183

Answer

Solution:

You haveIlluminate\Support\Collection object not array. You can do

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

UPD: There isprependmethod

$RecipientList->prepend('All', 'All');

People are also looking for solutions to the problem: php - User id won't save to database getting null

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.