php - Laravel Collections counting result -


on user model (table 4 records), when do:

$coll = user::all(); echo $coll->count(); 

i amount of records found (4).

but when do:

$coll = user::find(2); echo $coll->count(); 

i not 1 (as expect) amount of attributes in resulting collection (23 in case).

how can check if more 1 records found?


update:

ok, see difference in result between collection , model.

but real problem have detect if having model or collection result. depending on result perform changes on contents of fields in items (with map()) or model. how can detect if result model or collection?

if(count($coll) > 1) 

works, right approach?

here's what's going on code have there:

1. when calling user::all() you'll illuminate\database\eloquent\collection on can call count counts elements in collection so:

public function count() {     return count($this->items); } 

this return number of items in collection correctly expected.

2. when calling user::find(2) however, eloquent query builder not return collection, because check see how many results there are, , since passed one id you'll @ one result, return eloquent model instead. model not have count() method, when try call $coll->count(); go magic __call method class has implemented looks this:

public function __call($method, $parameters) {     if (in_array($method, array('increment', 'decrement')))     {         return call_user_func_array(array($this, $method), $parameters);     }      $query = $this->newquery();      return call_user_func_array(array($query, $method), $parameters); } 

as can see method tries see if should call couple of hardcoded methods (increment , decrement), of course don't match in case because $method = 'count', continues create new query on call count method.

the bottom line both first , second code samples end doing same thing: counting entries in users table.

and since, pointed our above, 1 id cannot match more 1 row (since ids unique), answer question there's no need or way count results of find(2), since can 0 (if null returned) or 1 (if model returned).


update

first of all, future reference can use php get_class determine class name of object or get_parent_class determine class extending. in case second function get_parent_class might useful determining model class since user class extends laravel abstract model class.

so if have model get_class($coll) report user, get_parent_class($coll) report \illuminate\database\eloquent\model.

now check if result collection or model can use instanceof:

instanceof used determine whether php variable instantiated object of class

your checks should this:

// check if it's collection if ($coll instanceof \illuminate\support\collection)  // check if it's model if ($coll instanceof \illuminate\database\eloquent\model) 

you might want check if result null, since find return null if no entry found given id:

if (is_null($coll)) 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -