簡體   English   中英

Php:Laravel - 使用雄辯的拔除方法后的數組推送

[英]Php : Laravel - Array push after using eloquent pluck method

這是我的查詢

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

哪能給我正確的結果,

但在執行查詢后,我還有1個key => value對來推送結果數組。

如果我打印當前結果,它是這樣的。

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..
        )

)

如果我試圖將我的Key => valye對推入這個數組,那就不行了。

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

需要輸出類似的東西

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..
            )

    )

這是因為$ RecipientList是Collection而不是Array。

試試這個

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

如果這不起作用,請嘗試以下代碼

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

希望這會幫助你。

你有Illuminate\\Support\\Collection對象而不是數組。 你可以做

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

UPD:有prepend方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM