@@ -12,27 +12,47 @@ class AnyModel extends Model
1212 public $ timestamps = false ;
1313 protected $ guarded = [];
1414
15+ /**
16+ * Set the tablename that the model will be loaded from.
17+ * @param string $tablename
18+ * @param array $options
19+ * @return AnyModel
20+ */
1521 public static function table ($ tablename , $ options = []){
1622
1723 // Create a new Model and set its table
1824 $ Model = new AnyModel ();
1925 $ Model ->table = $ tablename ;
2026
2127 // Set any further options passed in before returning
28+ // since AnyModel extends Model protected properties
29+ // on Model can be set here.
2230 Collect ($ options )->each (function ($ value , $ name ) use ($ Model ){
2331 $ Model ->{$ name } = $ value ;
2432 });
2533
2634 return $ Model ;
2735 }
36+
37+ /**
38+ * If used on table without id field, Model->id will not be available. If it is not set via
39+ * primaryKey and have it work so if it is requested and does not exist rather than
40+ * returning '' (default behaviour) raise an error.
41+ * @return mixed
42+ * @throws \Exception
43+ */
2844 public function getIDattribute (){
29- // due to various contortions Model->id will not always be available and it is not possible to set
30- // primaryKey and have it work so if it is requested and does not exist rather than returning '' (default behaviour)
31- // raise an error.
32- if (!isset ($ this ->{$ this ->primaryKey })){
45+
46+ // due to various contortions Model->id will not always be available. If it is not set via
47+ // primaryKey and have it work so if it is requested and does not exist, rather than
48+ // returning '' (default behaviour), raise an error.
49+ if (!isset ($ this ->attributes [$ this ->primaryKey ])){
3350 throw new \Exception ('AnyModel::id does not have a value. Please specify actual id field name ' );
3451 }
3552
53+ // return primary key value.
54+ return $ this ->attributes [$ this ->primaryKey ];
55+
3656 }
3757
3858}
0 commit comments