initializeFields(); } protected function initializeFields() { self::$imageFields = array( 'image', 'logo', 'signature', 'cover_photo', 'featured_image', 'image_thumb', 'primary_logo', 'secondary_logo', 'thumb_image', 'thumb', 'cover', 'photo', 'passport_copy', 'icon', 'favicon', 'og_image', 'no_image', ); self::$textEditorFields = array('details', 'description', 'text', 'qualification', 'experience', 'required_documents', 'biodata', 'privacy_policy', 'content1', 'content2', 'content3'); self::$textAreaFields = array('copyright', 'remarks', 'seo_keywords', 'seo_description', 'seo_descriptions', 'intro', 'meta_tags', 'google_map', 'og_tags'); self::$dateFields = array('dob', 'postedon', 'valid_till', 'startdate', 'enddate', 'from', 'to', 'starts', 'ends'); self::$passwordFields = array('password'); self::$HiddenCols = array("createdby", "updatedby", "updated_at", "created_at", "status", "alias", "display_order", "is_main", 'email_verified_at'); } protected function getImageFields() { return $this->imageFields; } protected function gettextEditorFields() { return $this->textEditorFields; } public function create() { $tables = DB::select('SHOW TABLES'); try { $databasdeName = DB::connection()->getDatabaseName(); $allTables = []; foreach ($tables as $table) { $table_field = "Tables_in_$databasdeName"; // dd($table->$table_field); if (strpos($table->$table_field, "_logs")) {continue;} $allTables[$table->$table_field] = $table->$table_field; } return view('crud.form.create', compact('allTables')); } catch (Exception $e) { dd($e->getMessage()); } } public function store(Request $request) { // dd($request->all()); $request->validate([ 'type' => 'required', 'tableName' => 'required', ]); $name = trim($request->type); $command = $name; $tableName = trim($request->tableName); $directoryName = trim($request->directoryName); switch ($command) { case 'dictonary': $data['tableName'] = "tbl_dictonary"; if ($tableName != "") { switch ($tableName) { case 'add': $data['TableCols'] = DB::select("describe " . $data['tableName']); return view("crud.settings.dictonary/add", $data); default: $data['TableRows'] = DB::select("select * from " . $data['tableName']); return view("crud.settings.dictonary.list", $data); } } $data['TableName'] = "tbl_dictonary"; $data['TableRows'] = DB::select("select * from " . $data['TableName']); return view("crud.settings.dictonary", $data); case 'curd': $data['tableName'] = $tableName; $data['directoryName'] = $directoryName; return view("crud.settings.curd", $data); break; case 'ajax-curd': $data['tableName'] = $tableName; $data['directoryName'] = $directoryName; $columns = DB::select("describe $tableName"); // users table return view("crud.settings.ajax-curd", $data); break; default: return view("crud.settings.home"); } } public function getTableNullablecreate() { return view("crud.settings.ajax-curd"); } public function tables() { $AllTables = DB::select('SHOW TABLES'); try { $databasdeName = DB::connection()->getDatabaseName(); $allTables = []; foreach ($AllTables as $table) { $myTable = new stdClass; //$allTables[]=$table['Tables_in_'.$databasdeName]; $key = "Tables_in_" . $databasdeName; $table->columns = DB::select("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $table->$key . "'"); $myTable->tablename = $table->$key; $myTable->tablecolumns = $table->columns; $allTables[] = $myTable; } return view('crud.form.tables', compact('allTables')); } catch (Exception $e) { dd($e->getMessage()); } } public static function getForeignTable($all_columns) { $string = '_id'; $foreign = []; foreach ($all_columns as $key => $column) { if (str_contains($column, $string) !== false) { // Yoshi version $foreign[] = $column; } } unset($foreign[0]); $strArray = []; foreach ($foreign as $key => $foreignKey) { $strArray[$key] = explode('_id', $foreignKey); unset($strArray[$key][1]); } if ($strArray) { $all_Foreign_Key_Table = call_user_func_array('array_merge', $strArray); foreach ($all_Foreign_Key_Table as $column) { $tableName[] = "tbl_" . $column; } return $tableName; } else { return []; } } public static function getTableColumns($TableName) { return array_column(DB::select("SHOW COLUMNS FROM $TableName"), 'Field'); } public static function ajaxShowContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableCols = DB::select("describe " . $TableName); $TableRows = DB::select("select * from " . $TableName); $folder = ''; $routeName = ''; if (!empty($directoryName)) { $folder .= strtolower($directoryName) . '/'; $routeName .= strtolower($directoryName) . '.'; } $routeName .= strtolower(str_replace("tbl_", "", $TableName)); $folder .= str_replace("tbl_", "", $TableName); $Table_pk = str_replace("tbl_", "", $TableName) . "_id"; $title = ucwords(str_replace("tbl_", "", $TableName)); $showContent = " "; foreach ($TableCols as $key => $TableCol): $TableCol = $TableCol->Field; if ($key == 0 || $TableCol == 'createdOn' || $TableCol == 'createdBy' || $TableCol == 'updatedBy' || $TableCol == 'created_at' || $TableCol == 'updated_at') { continue; } $TableColLabel = ucwords(str_replace("_", " ", $TableCol)); if ($TableCol == 'status') { $showContent .= "

Status :     $TableCol == 1 ? 'text-success' : 'text-danger'}}\">{{\$data->$TableCol == 1 ? 'Active' : 'Inactive'}}

"; } else { $showContent .= '

' . $TableColLabel . " :     {{\$data->$TableCol}}

"; } endforeach; $showContent .= "

Created On :   {{\$data->created_at}}

Created By :   {{\$data->createdBy}}

Updated On :   {{\$data->updated_at}}

Updated By :   {{\$data->updatedBy}}

"; return $showContent; } public static function ajaxEditContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableCols = DB::select("describe " . $TableName); $TableRows = DB::select("select * from " . $TableName); $folder = ''; $routeName = ''; if (!empty($directoryName)) { $folder .= strtolower($directoryName) . '/'; $routeName .= strtolower($directoryName) . '.'; } $routeName .= strtolower(str_replace("tbl_", "", $TableName)); $folder .= str_replace("tbl_", "", $TableName); $title = ucwords(str_replace("tbl_", "", $TableName)); $all_columns = self::getTableColumns($TableName); $Table_pk = $all_columns[0]; $all_foreign_table = self::getForeignTable($all_columns); $all_foreignKey = []; if ($all_foreign_table) { foreach ($all_foreign_table as $key => $tablename) { $all_foreignKey[$tablename] = self::getTableColumns($tablename); $all_foreignKey[$tablename] = $all_foreignKey[$tablename][0] ?? null; } } $editContent = "
$Table_pk]):route('$routeName.store')}}\" id=\"updateCustomForm\" method=\"POST\" >\n @csrf "; $editContent .= "" . PHP_EOL; $editContent .= '
'; foreach ($TableCols as $key => $TableCol): $TableCol = $TableCol->Field; if (!in_array($TableCol, self::$HiddenCols)) { $TableColLabel = ucwords(str_replace("_", " ", $TableCol)); switch ($TableCol) { case $TableCols[0]->Field: break; case (strpos($TableCol, "parent_") !== false): $editContent .= '
'; $editContent .= "{{createCustomSelect('$TableName', 'title', '" . $TableCols[0]->Field . "', \$editable?\$data->$TableCol:'', '" . $TableColLabel . "','$TableCol', 'form-control select2','status<>-1')}}"; $editContent .= "
"; break; case (strpos($TableCol, "s_id") !== false): //CASE TO HANDLE FOREIGN TABLE $FTNameRaw = str_replace("s_id", "s", $TableCol); $FTName = "tbl_" . str_replace("s_id", "s", $TableCol); $FTCols = DB::select("describe " . $FTName); $editContent .= '
'; //createCustomSelect($tableName, $fieldNameToDisplay, $fieldNameForValue, $defaultValueSelected, $displayTextForLabel, $HTMLElementName, $additionalClass = "form-control", $defaultCondition = null) $editContent .= "{{createCustomSelect('$FTName', 'title', '" . $FTCols[0]->Field . "', \$editable?\$data->$TableCol:'', '" . $TableColLabel . "','$TableCol', 'form-control select2','status<>-1')}}"; $editContent .= "
"; break; case (in_array($TableCol, self::$textEditorFields)): $editContent .= '
'; //function createTextArea($name, $class = "", $display = "", $default = "",$row = "") $editContent .= "{{createTextarea(\"$TableCol\",\"$TableCol ckeditor-classic\",\"$TableColLabel\",\$editable?\$data->$TableCol:'')}}\n"; $editContent .= "
"; break; case (in_array($TableCol, self::$imageFields)): $editContent .= '
'; $editContent .= "{{createImageInput(\"$TableCol\",\"$TableColLabel\",'',\$editable?\$data->$TableCol:'')}}\n"; $editContent .= "
"; break; case (in_array($TableCol, self::$dateFields)): $editContent .= '
'; $editContent .= "{{createDate(\"$TableCol\",\"$TableColLabel\",'',\$editable?\$data->$TableCol:'')}}\n"; //createDate($name, $display = "",$class = "datepicker", $default = "") $editContent .= "
"; break; case (in_array($TableCol, self::$passwordFields)): $editContent .= '
'; $editContent .= "{{createPassword(\"$TableCol\",\"$TableColLabel\",'',\$editable?\$data->$TableCol:'')}}\n"; //createDate($name, $display = "",$class = "datepicker", $default = "") $editContent .= "
"; break; case (in_array($TableCol, self::$textAreaFields)): $editContent .= '
'; $editContent .= "{{createPlainTextArea(\"$TableCol\",'',\"$TableColLabel\",\$editable?\$data->$TableCol:'')}}\n"; //createDate($name, $display = "",$class = "datepicker", $default = "") $editContent .= "
"; break; default: $editContent .= '
'; $editContent .= "{{createText(\"$TableCol\",\"$TableCol\",\"$TableColLabel\",'',\$editable?\$data->$TableCol:'')}}\n"; $editContent .= "
"; } } endforeach; $editContent .= '
'; $editContent .= "\n"; $editContent .= "\n"; $editContent .= "
"; $editContent .= " "; return $editContent; } //end of ajaxEditContent method public static function editContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableCols = DB::select("describe " . $TableName); $TableRows = DB::select("select * from " . $TableName); $folder = ''; $routeName = ''; if (!empty($directoryName)) { $folder .= strtolower($directoryName) . '/'; $routeName .= strtolower($directoryName) . '.'; } $routeName .= strtolower(str_replace("tbl_", "", $TableName)); $folder .= str_replace("tbl_", "", $TableName); $Table_pk = str_replace("tbl_", "", $TableName) . "_id"; $title = ucwords(str_replace("tbl_", "", $TableName)); $editContent = "@extends('layouts.app') @section('content')

{{ label('Edit $title') }}

\n
"; $editContent .= self::ajaxEditContent($TableName, $directoryName); $editContent .= "
\n@endsection"; $path = base_path() . "/resources/views/crud/generated/$folder/"; $file = $path . "edit.blade.php"; if (!file_exists("$path")) { mkdir("$path", 0777, true); } if (!file_exists($file)) { $file = fopen($file, 'w'); fwrite($file, $editContent); fclose($file); } return $editContent; } //end of editContent method public static function addContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableCols = DB::select("describe " . $TableName); $TableRows = DB::select("select * from " . $TableName); $folder = ''; $routeName = ''; if (!empty($directoryName)) { $folder .= strtolower($directoryName) . '/'; $routeName .= strtolower($directoryName) . '.'; } $routeName .= strtolower(str_replace("tbl_", "", $TableName)); $folder .= str_replace("tbl_", "", $TableName); $Table_pk = str_replace("tbl_", "", $TableName) . "_id"; $title = ucwords(str_replace("tbl_", "", $TableName)); $addContent = "@extends('layouts.app') @section('content')

{{ label('Add $title') }}

\n
"; $addContent .= self::ajaxEditContent($TableName, $directoryName); $addContent .= "
\n@endsection"; $path = base_path() . "/resources/views/crud/generated/$folder/"; $file = $path . "edit.blade.php"; if (!file_exists("$path")) { mkdir("$path", 0777, true); } if (!file_exists($file)) { $file = fopen($file, 'w'); fwrite($file, $addContent); fclose($file); } return $addContent; } //End of addContent /* public static function ajaxAddContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableCols = DB::select("describe " . $TableName); $TableRows = DB::select("select * from " . $TableName); $folder = ''; $routeName = ''; if (!empty($directoryName)) { $folder .= strtolower($directoryName) . '/'; $routeName .= strtolower($directoryName) . '.'; } $routeName .= strtolower(str_replace("tbl_", "", $TableName)); $folder .= str_replace("tbl_", "", $TableName); $Table_pk = str_replace("tbl_", "", $TableName) . "_id"; $title = ucwords(str_replace("tbl_", "", $TableName)); $all_columns = self::getTableColumns($TableName); $all_foreign_table = self::getForeignTable($all_columns); $all_foreignKey = []; if ($all_foreign_table) foreach ($all_foreign_table as $key => $tablename) { $all_foreignKey[$tablename] = self::getTableColumns($tablename); $all_foreignKey[$tablename] = $all_foreignKey[$tablename][0] ?? null; } $addContent = "
$Table_pk]):route('$routeName.store')}}\" id=\"storeCustomForm\" method=\"POST\">\n @csrf \n"; $addContent .= '
'; foreach ($TableCols as $key => $TableCol): $TableCol = $TableCol->Field; if (!in_array($TableCol, self::$HiddenCols)) { $TableColLabel = ucwords(str_replace("_", " ", $TableCol)); switch ($TableCol) { case $TableCols[0]->Field: break; case (strpos($TableCol, "parent_") !== false): $addContent .= '
'; $addContent .= "{{createCustomSelect('$TableName', 'title', '" . $TableCols[0]->Field . "', '', '" . $TableColLabel . "','$TableCol', 'form-control select2','status<>-1')}}"; $addContent .= "
"; break; case (strpos($TableCol, "s_id") !== false): //CASE TO HANDLE FOREIGN TABLE $FTNameRaw = str_replace("s_id", "s", $TableCol); $FTName = "tbl_" . str_replace("s_id", "s", $TableCol); $FTCols = DB::select("describe " . $FTName); $addContent .= '
'; //createCustomSelect($tableName, $fieldNameToDisplay, $fieldNameForValue, $defaultValueSelected, $displayTextForLabel, $HTMLElementName, $additionalClass = "form-control", $defaultCondition = null) $addContent .= "{{createCustomSelect('$FTName', 'title', '" . $FTCols[0]->Field . "', '', '" . $TableColLabel . "','$TableCol', 'form-control select2','status<>-1')}}"; $addContent .= "
"; break; case (in_array($TableCol, self::$textEditorFields)): $addContent .= '
'; $addContent .= "{{createTextarea(\"$TableCol\",\"$TableCol ckeditor-classic\",\"$TableColLabel\")}}\n"; $addContent .= "
"; break; case (in_array($TableCol, self::$textAreaFields)): $addContent .= '
'; $addContent .= "{{createPlainTextArea(\"$TableCol\",\"$TableCol \",\"$TableColLabel\")}}\n"; $addContent .= "
"; break; case (in_array($TableCol, self::$imageFields)): $addContent .= '
'; $addContent .= "{{createImageInput(\"$TableCol\",\"$TableColLabel\")}}\n"; $addContent .= "
"; break; case (in_array($TableCol, self::$dateFields)): $addContent .= '
'; $addContent .= "{{createDate(\"$TableCol\",\"$TableColLabel\",'',date('Y-m-d'))}}\n"; //createDate($name, $display = "",$class = "datepicker", $default = "") $addContent .= "
"; break; case (in_array($TableCol, self::$passwordFields)): $addContent .= '
'; $addContent .= "{{createPassword(\"$TableCol\",\"$TableColLabel\",'')}}\n"; //createDate($name, $display = "",$class = "datepicker", $default = "") $addContent .= "
"; break; default: $addContent .= '
'; $addContent .= "{{createText(\"$TableCol\",\"$TableCol\",\"$TableColLabel\")}}\n"; $addContent .= "
"; } } endforeach; $addContent .= '
'; $addContent .= "\n"; $addContent .= "\n"; $addContent .= "
"; $addContent .= " "; return $addContent; } //End of ajax addContent */ public static function showContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableCols = DB::select("describe " . $TableName); $TableRows = DB::select("select * from " . $TableName); $folder = ''; $routeName = ''; if (!empty($directoryName)) { $folder .= strtolower($directoryName) . '/'; $routeName .= strtolower($directoryName) . '.'; } $routeName .= strtolower(str_replace("tbl_", "", $TableName)); $folder .= str_replace("tbl_", "", $TableName); $Table_pk = str_replace("tbl_", "", $TableName) . "_id"; $title = ucwords(str_replace("tbl_", "", $TableName)); $showContent = "@extends('layouts.app') @section('content')

\n
"; $showContent .= self::ajaxShowContent($TableName, $directoryName); $showContent .= "
@endSection"; $path = base_path() . "/resources/views/crud/generated/$folder/"; $file = $path . "show.blade.php"; if (!file_exists("$path")) { mkdir("$path", 0777, true); } if (!file_exists($file)) { $file = fopen($file, 'w'); fwrite($file, $showContent); fclose($file); } return $showContent; } public static function migrationContent($tableName) { $tableName = strtolower($tableName); $columns = self::getTableColumns($tableName); $tableFields = DB::select("describe $tableName"); // foreach($tableFields as $tableField){ // dd($tableField->Field); // } $pkField = str_replace("tbl_", "", $tableName) . "_id"; //BibClass::pre($tableFields); $contentString = "Type; //WRITE CODE HERE TO IDENTIFY THE TYPES OF FIELDS switch ($Type) { case 'int(11)': if ($tableField->Field == $pkField): $contentString .= "\$table->integer(\"$tableField->Field\")->autoIncrement();\n"; else: $contentString .= "\$table->integer(\"$tableField->Field\")->default(0);\n"; endif; break; case 'varchar(255)': $contentString .= "\$table->string(\"$tableField->Field\")->nullable();\n"; break; case 'datetime': $contentString .= "\$table->datetime(\"$tableField->Field\")->nullable();\n"; break; case 'text': $contentString .= "\$table->text(\"$tableField->Field\")->nullable();\n"; break; case 'timestamp': $contentString .= "\$table->text(\"$tableField->Field\")->nullable();\n"; break; default: } } $contentString .= "\$table->timestamps();\n });\n }\n public function down()\n {\n Schema::dropIfExists(\"$tableName\");\n }\n};"; /* $file = fopen(base_path() . "/database/migrations/" . date('Y_m_d_his') . "_create_" . $tableName . "_table.php", 'w'); fwrite($file, $contentString); fclose($file); */ return $contentString; } public static function routeContent($TableName, $directoryName) { $TableName = strtolower($TableName); $TableName = str_replace("tbl_", "", $TableName); $routePrefixName = ''; $ControllerName = ''; if (!empty($directoryName)) { // $ControllerName .= ucwords($directoryName) . '/'; $routePrefixName .= strtolower($directoryName) . '.'; } $ControllerName .= ucwords($TableName) . "Controller"; $routePrefixName .= strtolower($TableName); $RouteContent = "Route::prefix(\"$TableName\")->group(function () { Route::get('/', [$ControllerName::class, 'index'])->name('$routePrefixName.index'); Route::get('/create', [$ControllerName::class, 'create'])->name('$routePrefixName.create'); Route::post('/store', [$ControllerName::class, 'store'])->name('$routePrefixName.store'); Route::post('/sort', [$ControllerName::class, 'sort'])->name('$routePrefixName.sort'); Route::post('/updatealias', [$ControllerName::class, 'updatealias'])->name('$routePrefixName.updatealias'); Route::get('/show/{id}', [$ControllerName::class, 'show'])->name('$routePrefixName.show'); Route::get('/edit/{id}', [$ControllerName::class, 'edit'])->name('$routePrefixName.edit') ; Route::post('/update/{id}', [$ControllerName::class, 'update'])->name('$routePrefixName.update'); Route::get('/destroy/{id}', [$ControllerName::class, 'destroy'])->name('$routePrefixName.destroy'); Route::get('/toggle/{id}', [$ControllerName::class, 'toggle'])->name('$routePrefixName.toggle'); Route::get('/clone/{id}', [$ControllerName::class, 'clone'])->name('$routePrefixName.clone'); });"; $path = base_path() . "/routes/CRUDgenerated/"; $file = $path . "route." . $routePrefixName . ".php"; if (!file_exists($path)) { mkdir($path, 0777, true); } if (!file_exists($file)) { $file = fopen($file, 'w'); fwrite($file, "

{{ label("' . $title . ' List") }}

{{label("Create New")}}
' . PHP_EOL; foreach ($columns as $key => $column) { if (!in_array($column, $HiddenColumns)) { switch ($column) { case (strpos($column, "parent_") !== false): $listContent .= '' . PHP_EOL; break; case (strpos($column, "s_id") !== false): $ParentName = str_replace("s_id", "s", $column); $listContent .= '' . PHP_EOL; break; default: $listContent .= '' . PHP_EOL; } } } $listContent .= ' @php $i = 1; @endphp @foreach ($data as $item) "> '; foreach ($columns as $key => $column) { if (!in_array($column, $HiddenColumns)) { switch ($column) { case (strpos($column, "parent_") !== false): $listContent .= '' . PHP_EOL; break; case (strpos($column, "s_id") !== false): //CASE TO HANDLE FOREIGN TABLE $FTNameRaw = str_replace("s_id", "s", $column); $FTName = "tbl_" . str_replace("s_id", "s", $column); $FTCols = DB::select("describe " . $FTName); $listContent .= '"; break; case 'alias': $listContent .= '' . PHP_EOL; break; case (in_array($column, self::$imageFields)): $listContent .= '' . PHP_EOL; break; case (in_array($column, self::$dateFields)): $listContent .= '' . PHP_EOL; break; case 'status': $listContent .= '' . PHP_EOL; break; default: $listContent .= '' . PHP_EOL; } } } $listContent .= ' @endforeach
{{label("Sn.")}}{{ label("Parent") }}{{ label("' . $ParentName . '") }}{{ label("' . $column . '") }}{{ label("Action") }}
{{ $i++ }}' . PHP_EOL; $listContent .= '{!! getFieldData("' . $TableName . '", "title", "' . $columns[0] . '", $item->' . $column . ') !!}' . PHP_EOL; $listContent .= '' . PHP_EOL; //function getFieldData($tableName, $returnField, $referenceFieldName, $referenceValue) $listContent .= '{!! getFieldData("' . $FTName . '", "title", "' . $FTCols[0]->Field . '", $item->' . $column . ') !!}' . PHP_EOL; $listContent .= "
{{ $item->alias }}
change alias
{{ showImageThumb($item->' . $column . ') }}{{ myDate($item->' . $column . ') }}{!! $item->status_name !!}{{ $item->' . $column . ' }}
@endsection @push("css") @endpush @push("js") @endpush '; $path = base_path() . "/resources/views/crud/generated/$folder/"; $file = $path . "index.blade.php"; if (!file_exists("$path")) { mkdir("$path", 0777, true); } if (!file_exists($file)) { $file = fopen($file, 'w'); fwrite($file, $listContent); fclose($file); } return $listContent; } //End of list code content //ajax complete List public static function modelContent($tableName, $directoryName) { $tableName = strtolower($tableName); $modelPath = ''; if (!empty($directoryName)) { $modelPath .= "\\" . ucfirst($directoryName); } $modelClass = ucfirst(str_replace("tbl_", "", $tableName)); $tableFields = DB::select("describe $tableName"); $pkField = $tableFields[0]->Field; $skipPRimaryKey = 1; $tableColumns = ''; foreach ($tableFields as $tableField): if ($skipPRimaryKey == 1) { $skipPRimaryKey = 2; continue; } $tableColumns .= "'" . $tableField->Field . "',\n"; endforeach; $contentString = "status == 1 ? ' Active ' : 'Inactive'; } protected function createdBy(): Attribute { return Attribute::make( get: fn (\$value) => User::find(\$value) ? User::find(\$value)->name : '', ); } protected function updatedBy(): Attribute { return Attribute::make( get: fn (\$value) => User::find(\$value) ? User::find(\$value)->name : '', ); } }"; $modelPath = ''; if (!empty($directoryName)) { $modelPath .= ucfirst($directoryName) . '/'; } if (!file_exists(base_path() . '/app/Models/' . $modelPath)) { mkdir(base_path() . '/app/Models/' . $modelPath, 0777, true); } $filename = $modelClass . ".php"; if (!file_exists(base_path() . "/app/Models/" . $modelPath . $filename)) { $file = fopen(base_path() . "/app/Models/" . $modelPath . $filename, 'w'); fwrite($file, $contentString); fclose($file); } return $contentString; } public static function controllerContent($tableName, $directoryName) { $tableName = strtolower($tableName); $tableFields = DB::select("describe $tableName"); $pkField = $tableFields[0]->Field; $controllerName = ucfirst(str_replace("tbl_", "", $tableName)) . "Controller"; $viewName = ''; $controllerPath = ''; $modelName = ''; if (!empty($directoryName)) { $viewName .= strtolower($directoryName) . '.'; $controllerPath .= "\\" . ucfirst($directoryName); $modelName .= ucfirst($directoryName) . '\\'; } $modelName .= ucfirst(str_replace("tbl_", "", $tableName)); $viewName .= strtolower(str_replace("tbl_", "", $tableName)); $modelClass = ucfirst(str_replace("tbl_", "", $tableName)); $tableAliasColumnName = "title"; $contentString = "modelService = new CommonModelService(\$model); } public function index(Request \$request) { createActivityLog($controllerName::class, 'index', '$directoryName $modelClass index'); \$data = $modelClass::where('status','<>',-1)->orderBy('display_order')->get(); return view(\"crud.generated.$viewName.index\", compact('data')); } public function create(Request \$request) { createActivityLog($controllerName::class, 'create', '$directoryName $modelClass create'); \$TableData = $modelClass::where('status','<>',-1)->orderBy('display_order')->get(); \$editable=false; return view(\"crud.generated.$viewName.edit\",compact('TableData','editable')); } public function store(Request \$request) { createActivityLog($controllerName::class, 'store', '$directoryName $modelClass store'); \$validator = Validator::make(\$request->all(), [ //ADD REQUIRED FIELDS FOR VALIDATION ]); if (\$validator->fails()) { return response()->json([ 'error' => \$validator->errors(), ],500); } \$request->request->add(['alias' => slugify(\$request->$tableAliasColumnName)]); \$request->request->add(['display_order' => getDisplayOrder('" . $tableName . "')]); \$request->request->add(['created_at' => date(\"Y-m-d h:i:s\")]); \$request->request->add(['updated_at' => date(\"Y-m-d h:i:s\")]); \$requestData=\$request->all(); array_walk_recursive(\$requestData, function (&\$value) { \$value = str_replace(env('APP_URL').'/', '', \$value); }); array_walk_recursive(\$requestData, function (&\$value) { \$value = str_replace(env('APP_URL'), '', \$value); }); DB::beginTransaction(); try { \$operationNumber = getOperationNumber(); \$this->modelService->create(\$operationNumber, \$operationNumber, null, \$requestData); } catch (\Exception \$e) { DB::rollBack(); Log::info(\$e->getMessage()); createErrorLog($controllerName::class, 'store', \$e->getMessage()); return response()->json(['status' => false, 'message' => \$e->getMessage()], 500); } DB::commit(); if (\$request->ajax()) { return response()->json(['status' => true, 'message' => 'The $modelClass Created Successfully.'], 200); } return redirect()->route('$viewName.index')->with('success','The $modelClass created Successfully.'); } public function sort(Request \$request) { \$idOrder = \$request->input('id_order'); foreach (\$idOrder as \$index => \$id) { \$companyArticle = $modelClass::find(\$id); \$companyArticle->display_order = \$index + 1; \$companyArticle->save(); } return response()->json(['status' => true, 'content' => 'The articles sorted successfully.'], 200); } public function updatealias(Request \$request) { \$articleId = \$request->input('articleId'); \$newAlias = \$request->input('newAlias'); \$companyArticle = $modelClass::find(\$articleId); if (!\$companyArticle) { return response()->json(['status' => false, 'content' => 'Company article not found.'], 404); } \$companyArticle->alias = \$newAlias; \$companyArticle->save(); return response()->json(['status' => true, 'content' => 'Alias updated successfully.'], 200); } public function show(Request \$request, \$id) { createActivityLog($controllerName::class, 'show', '$directoryName $modelClass show'); \$data = $modelClass::findOrFail(\$id); return view(\"crud.generated.$viewName.show\", compact('data')); } public function edit(Request \$request, \$id) { createActivityLog($controllerName::class, 'edit', '$directoryName $modelClass edit'); \$TableData = $modelClass::where('status','<>',-1)->orderBy('display_order')->get(); \$data = $modelClass::findOrFail(\$id); \$editable=true; return view(\"crud.generated.$viewName.edit\", compact('data','TableData','editable')); } public function update(Request \$request, \$id) { createActivityLog($controllerName::class, 'update', '$directoryName $modelClass update'); \$validator = Validator::make(\$request->all(), [ //ADD VALIDATION FOR REQIRED FIELDS ]); if (\$validator->fails()) { return response()->json([ 'error' => \$validator->errors(), ],500); } \$requestData=\$request->all(); array_walk_recursive(\$requestData, function (&\$value) { \$value = str_replace(env('APP_URL').'/', '', \$value); }); array_walk_recursive(\$requestData, function (&\$value) { \$value = str_replace(env('APP_URL'), '', \$value); }); DB::beginTransaction(); try { \$OperationNumber = getOperationNumber(); \$this->modelService->update(\$OperationNumber, \$OperationNumber, null, \$requestData, \$request->input('" . $pkField . "')); } catch (Exception \$e) { DB::rollBack(); Log::info(\$e->getMessage()); createErrorLog($controllerName::class, 'update', \$e->getMessage()); return response()->json(['status' => false, 'message' => \$e->getMessage()], 500); } DB::commit(); if (\$request->ajax()) { return response()->json(['status' => true, 'message' => 'The $modelClass updated Successfully.'], 200); } // return redirect()->route('$viewName.index')->with('success','The $modelClass updated Successfully.'); return redirect()->back()->with('success', 'The $modelClass updated successfully.'); } public function destroy(Request \$request,\$id) { createActivityLog($controllerName::class, 'destroy', '$directoryName $modelClass destroy'); DB::beginTransaction(); try { \$OperationNumber = getOperationNumber(); \$this->modelService->destroy(\$OperationNumber, \$OperationNumber, \$id); } catch (Exception \$e) { DB::rollBack(); Log::info(\$e->getMessage()); createErrorLog($controllerName::class, 'destroy', \$e->getMessage()); return response()->json(['status' => false, 'message' => \$e->getMessage()], 500); } DB::commit(); return response()->json(['status'=>true,'message'=>'The $modelClass Deleted Successfully.'],200); } public function toggle(Request \$request,\$id) { createActivityLog($controllerName::class, 'destroy', '$directoryName $modelClass destroy'); \$data = $modelClass::findOrFail(\$id); \$requestData=['status'=>(\$data->status==1)?0:1]; DB::beginTransaction(); try { \$OperationNumber = getOperationNumber(); \$this->modelService->update(\$OperationNumber, \$OperationNumber, null, \$requestData, \$id); } catch (Exception \$e) { DB::rollBack(); Log::info(\$e->getMessage()); createErrorLog($controllerName::class, 'destroy', \$e->getMessage()); return response()->json(['status' => false, 'message' => \$e->getMessage()], 500); } DB::commit(); return response()->json(['status'=>true,'message'=>'The $modelClass Deleted Successfully.'],200); } public function clone(Request \$request,\$id) { createActivityLog($controllerName::class, 'clone', '$directoryName $modelClass clone'); \$data = $modelClass::findOrFail(\$id); unset(\$data['updatedby']); unset(\$data['createdby']); \$requestData=\$data->toArray(); DB::beginTransaction(); try { \$OperationNumber = getOperationNumber(); \$this->modelService->create(\$OperationNumber, \$OperationNumber, null, \$requestData); } catch (Exception \$e) { DB::rollBack(); Log::info(\$e->getMessage()); createErrorLog($controllerName::class, 'clone', \$e->getMessage()); return response()->json(['status' => false, 'message' => \$e->getMessage()], 500); } DB::commit(); return response()->json(['status'=>true,'message'=>'The $modelClass Clonned Successfully.'],200); } } "; // $filename = ($controllerPath != "") ? $controllerPath . "/" : ""; $controllerDirectory = ''; if (!empty($controllerPath)) { $controllerDirectory = ucfirst($directoryName) . '/'; } $filename = $controllerName . ".php"; $filePath = base_path() . "/app/Http/Controllers/" . $controllerDirectory . $filename; if (!file_exists($filePath)) { if (!file_exists(base_path() . '/app/Http/Controllers/' . $controllerDirectory)) { mkdir(base_path() . '/app/Http/Controllers/' . $controllerDirectory, 0777, true); } $file = fopen($filePath, 'w'); fwrite($file, $contentString); fclose($file); } return $contentString; } }