diff src/Generator/Model/stubs/model.stub @ 26:555bfaa500ac codex

Big update for view creation based on the column type.
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 15 May 2025 21:50:38 -0400
parents 31109c61ce02
children f88d2d5dee30
line wrap: on
line diff
--- a/src/Generator/Model/stubs/model.stub	Sun May 11 21:03:51 2025 -0400
+++ b/src/Generator/Model/stubs/model.stub	Thu May 15 21:50:38 2025 -0400
@@ -27,21 +27,25 @@
 			# {{ atributeInsertPoint }}
     ];
 
-		protected $fillable =[
+		protected $fillable = [
 			# {{ fillableInsertPoint }}
 		];
 
+		protected $default_relations = [
+			# {{ defaultRelationsInsertPoint }}
+		];
 
+		//MARK FOR MODEL
 		public static function boot() : void {
 			parent::boot();
 
 			self::creating(function ($item) {
-				$item->created_by = \Auth::user()->id;
-				$item->updated_by = \Auth::user()->id;
+				$item->created_by = \Auth::user()?->id ?? '';
+				$item->updated_by = \Auth::user()?->id ?? '';
 			});
 
 			self::saving(function ($item) {
-				$item->updated_by = \Auth::user()->id;
+				$item->updated_by = \Auth::user()?->id ?? '';
 			});
 		}
 
@@ -57,4 +61,76 @@
     // HasManyThrough
 		# {{ has_many_through_relationships }}
 
+
+		/**
+		 * Load the default relations for the model.
+		 *
+		 * @return $this
+		 */
+		public function load_relations() {
+			foreach($this->default_relations as $relation) {
+				$this->load($relation);
+			}
+			return $this;
+		}
+
+		//MARK FOR MODEL
+		protected static function load_auxilary_data() {
+			$data = [];
+
+			$instance = new static();
+		
+		  foreach($instance->default_relations as $relation) {
+				$related_model = $instance->$relation()->getRelated();
+				$related_table = $related_model->getTable();
+				$data[$related_table] = $related_model->all()->pluck('name','id')->toArray();
+			}
+
+			return $data;
+		}
+
+		//MARK FOR MODEL
+		public static function load_index() {
+			return static::load_auxilary_data();
+		}
+
+		//MARK FOR MODEL
+		public static function load_create() {
+			return static::load_auxilary_data();
+		}
+
+		//MARK FOR MODEL
+		public static function load_edit() {
+			return static::load_auxilary_data();
+		}
+
+
+		/**
+		 * Retrieve a query builder instance with default relations loaded.
+		 *
+		 * @return \Illuminate\Database\Eloquent\Builder
+		 */
+		//MARK FOR MODEL
+		public static function data_query() {
+		    $query = static::query();
+		
+				$instance = new static();
+
+		    foreach($instance->default_relations as $relation) {
+		        $query->with($relation);
+		    }
+		
+		    return $query;
+		}
+
+		/**
+     * Retrieve a query builder instance with default relations loaded.
+     *
+     * @return \Illuminate\Database\Eloquent\Builder
+     */
+    public static function get_data()
+    {
+        return static::data_query()->get();
+    }
+
 }