has a wrap method and array_wrap() helper to normalize values into an array. Raul @rcubitto shared this nice tip about it on Twitter and before seeing his Tweet I wasn’t aware of this method:

I noticed that some people asked in response to Raul’s tweet why that was needed vs. casting to an array:

$value = (array) $value;

Typecasting works for primitive values, but “iterables” get treated differently. For example, let’s say you want to allow a user to pass either one Eloquent model or an array of models. Here’s what happens when you try to cast a single model to an array with (array):

>>> $u = AppUser::create([
    'name' => 'Admin',
    'email' => 'admin@example.com',
    'password' => bcrypt('secret')
]);
>>> (array) $u
=> [
     "

LEAVE A REPLY

Please enter your comment!
Please enter your name here