Page MenuHomeDevCentral

FindableByAttribute.php
No OneTemporary

FindableByAttribute.php

<?php namespace AuthGrove\Services;
trait FindableByAttribute {
/**
* Finds an object by the specified attribute and value
*
* @param string $attribute The attribute to find
* @param string $value The value the object should have for the specified attribute to match
* @return Model|null If the object has been found, an instance of the class. Otherwise, null
*
* e.g. User::findBy('username', 'quux')
*
* If the attribute isn't unique, this method will return the first object found.
*/
static function findBy ($attribute, $value) {
$method = 'static::where' . ucfirst($attribute);
$object = call_user_func($method, $value)
->first(['*']);
return $object;
}
}

File Metadata

Mime Type
text/x-php
Expires
Mon, Nov 17, 15:46 (17 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3167182
Default Alt Text
FindableByAttribute.php (761 B)

Event Timeline