DEV Community

Morcos Gad
Morcos Gad

Posted on • Edited on

Laravel Arr prepend() function

Let's start quickly, I found the prepend function and I want to share it with you
I will explain you how to work laravel array prepend() function. We will show example of array prepend function to get array prepend element value in laravel.The Arr::prepend method will push an item onto the beginning of an array

$array = ['php', 'laravel', 'html','css'];
$prepended = Arr::prepend($array,'Vue');
print_r($prepended ); //['Vue', 'php', 'laravel', 'html', 'css']
Enter fullscreen mode Exit fullscreen mode

Other use for Dropsdown

User::pluck('name','id')->prepend('Plz Select','');
/*
 all: [ 
  "" => "Plz Select"
  1 => "User k",
  2 => "Euna Dau",
  ......
 ]
*/
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code.

Top comments (2)

Collapse
 
andersbjorkland profile image
Anders Björkland

Neat!
By the way, should the print_r maybe output 'Vue' before 'php'?

Collapse
 
morcosgad profile image
Morcos Gad

You are indeed correct, I modified the article, thanks for the comment.