iambib/collection-extender

Laravel/Lumen 9+ Collection Extender

1.0.3 2025-08-18 06:02 UTC

This package is auto-updated.

Last update: 2025-08-18 06:03:08 UTC


README

This is a set of Illuminate\Support\Collection items I use on a daily bases

Install

REQUIREMENTS

Laravel / Lumen version 9+

Installation

composer  require  iambib/collection-extender

Currently available


$collection->recursive();

$collection->pushToKey($key, $value, ?$recursive);

$collection->toModel(?$model_class);

$collection->input($keys,?$default);

$collection->morphTo($object_class)

$collection->whereArrayContains('foo', 'bar');

$collection->whereArrayDoesntContain('foo', 'bar');

Examples

1. $collection->recursive()

From array / multidimensional array

$array = [

'foo'=>'bar',

'bar'=>[

'foo'=>'Lorem',

'bar'=>'Ipsum',

],

];

to Illuminate\Support\Collection

Illuminate\Support\Collection {#354 ▼

#items: array:2 [▼

"foo" => "bar"

"bar" => Illuminate\Support\Collection {#358 ▼

#items: array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

#escapeWhenCastingToString: false

}

]

#escapeWhenCastingToString: false

}

2. $collection->pushToKey('foo', 'newValue')

From single key value

Illuminate\Support\Collection {#354 ▼

#items: array:2 [▼

"foo" => "bar"

"bar" => Illuminate\Support\Collection {#358 ▼

#items: array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

#escapeWhenCastingToString: false

}

]

#escapeWhenCastingToString: false

}

To array key value

Illuminate\Support\Collection {#354 ▼

#items: array:2 [▼

"foo" => array:2 [▼

0 => "bar"

1 => "newValue"

]

"bar" => Illuminate\Support\Collection {#358 ▼

#items: array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

#escapeWhenCastingToString: false

}

]

#escapeWhenCastingToString: false

}

3. $collection->toModel works in 2 different ways

With model class as param

$collection->toModel(\App\Models\User::class)

From collection

Illuminate\Support\Collection {#352 ▼

#items: array:2 [▼

"foo" => "bar"

"bar" => array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

]

#escapeWhenCastingToString: false

}

or without model class as param

$collection->toModel()

Which will look for the key that is configured in collection-callback.php as a model_class_key

Config

return [

'model_class_key' => 'model_name',

];



From collection

Illuminate\Support\Collection {#353 ▼ // packages/iambib/collection-extender/src/Providers/AppServiceProvider.php:78

#items: array:3 [▼

...

"model_name" => "App\Models\User"

]

#escapeWhenCastingToString: false

}

To Model

App\Models\User {#360 ▼

#attributes: array:2 [▼

"foo" => "bar"

"bar" => array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

]

#hidden: []

#visible: []

#appends: []

#fillable: []

#guarded: []

#casts: []

#rememberTokenName: "remember_token"

#autoload_relations: []

}

4. $collection->input('bar.foo')

$default can be string or callback

From collection

Illuminate\Support\Collection {#352 ▼

#items: array:2 [▼

"foo" => "bar"

"bar" => array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

]

#escapeWhenCastingToString: false

}

Returns

"Lorem"

$collection->input('bar.foo.empty','Not found')

Returns

"Not found"

5. $collection->morphTo(ApiResponse::class)

Morphs current collection into an object that you need
From collection

Illuminate\Support\Collection {#354 ▼

#items: array:3 [▼

"status" => true

"data" => array:2 [▼

"foo" => "bar"

"bar" => array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

]

"message" => "Success"

]

#escapeWhenCastingToString: false

}

to ApiResponse

ApiResponse {#361 ▼

#status: true

#data: array:2 [▼

"foo" => "bar"

"bar" => array:2 [▼

"foo" => "Lorem"

"bar" => "Ipsum"

]

]

#message: "Success"

#errors: null

}

5. $collection->whereArrayContains('foo', 'bar')

Searches bar inside foo.
From collection

  #items: array:2 [▼
    0 => array:4 [▼
      ...
      "foo" => array:2 [▼
        0 => "bar"
        1 => "tar"
      ]
    ]
    1 => array:4 [▼
      ...
      "foo" => array:1 [▼
        0 => "tar"
      ]
    ]
  ]
  #escapeWhenCastingToString: false }

Returns

items: array:1 [▼
    0 => array:4 [▼
      ...
      "foo" => array:2 [▼
        0 => "bar"
        1 => "tar"
      ]
    ]
  ]
  #escapeWhenCastingToString: false }

5. $collection->whereArrayDoesntContain('foo', 'bar')

Searches bar inside foo.
From collection

  #items: array:2 [▼
    0 => array:4 [▼
      ...
      "foo" => array:2 [▼
        0 => "bar"
        1 => "tar"
      ]
    ]
    1 => array:4 [▼
      ...
      "foo" => array:1 [▼
        0 => "tar"
      ]
    ]
  ]
  #escapeWhenCastingToString: false }

Returns

items: array:1 [▼
    0 => array:4 [▼
      ...
      "foo" => array:2 [▼
        1 => "tar"
      ]
    ]
  ]
  #escapeWhenCastingToString: false }

Support

Hey dude! If you like it .. well beers or a coffee would be nice :D

coffee