Check variable is a Laravel collection

Panjeh
Jan 14, 2021

--

In general, you can check simply using:

use Illuminate\Support\Collection;

....
if($variable instanceof Collection) {
....
}

In fact, we are checking whether the $variableis a Collection-object or not.

Another example:

We know that Illuminate\Database\Eloquent\Collection extends from Illuminate\Support\Collection

Suppose:

$record = User::all();

Then:

dump($record instanceof \App\User); // returns false

While:

dump($record instanceof \Illuminate\Database\Eloquent\Collection); // returns true

I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

--

--

Panjeh
Panjeh

Written by Panjeh

Posting about Python and Laravel

No responses yet