Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Services/FindableByAttribute.php b/app/Services/FindableByAttribute.php
new file mode 100644
index 0000000..92697a7
--- /dev/null
+++ b/app/Services/FindableByAttribute.php
@@ -0,0 +1,21 @@
+<?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-diff
Expires
Tue, Oct 6, 02:37 (42 m, 42 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4152222
Default Alt Text
(989 B)

Event Timeline