0

If there is a way to check in laravel 7 if given var is collection of (Specific) models?

I tried

gettype($collectionVar) 

and it returns ‘object’ string value...

Thanks!

Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • Possible values for the returned **gettype()** are: `"boolean"`, `"integer"`, `"double"`, `"float"`, `"string"`, `"array"`, `"object"`, `"resource"`, `"NULL"`, `"unknown type"` . So Model instance and collection both give you gettype() as `"object"` is that case – STA Oct 10 '20 at 11:41
  • 1
    Does this answer your question? [Laravel get class name of related model](https://stackoverflow.com/questions/26292718/laravel-get-class-name-of-related-model) – Deepesh Thapa Oct 10 '20 at 11:41

1 Answers1

1

You can take the first Model in the collection and check the instance type

use App\Models\Specific

if ($collection->first() instanceof Specific) {
    // code here
}