coolsam726 /
jetstream-inertia-generator
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | @php echo "<?php"; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It is not recommend to use PHP's short opening tag
<?, better use <?php, or <?= in case of outputting.
Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed. As a precaution to avoid these problems better use the long opening tag Loading history...
|
|||
| 2 | @endphp |
||
| 3 | |||
| 4 | namespace {{ $repoNamespace }}; |
||
| 5 | |||
| 6 | use {{$modelFullName}}; |
||
| 7 | use DB; |
||
| 8 | use Illuminate\Http\Request; |
||
| 9 | use Illuminate\Support\Str; |
||
| 10 | use Yajra\DataTables\DataTables; |
||
| 11 | use Yajra\DataTables\Html\Column; |
||
| 12 | |||
| 13 | class {{ $repoBaseName }} |
||
| 14 | { |
||
| 15 | private {{$modelBaseName}} $model; |
||
| 16 | public static function init({{$modelBaseName}} $model): {{$repoBaseName}} |
||
| 17 | { |
||
| 18 | $instance = new self; |
||
| 19 | $instance->model = $model; |
||
| 20 | return $instance; |
||
| 21 | } |
||
| 22 | |||
| 23 | public static function store(object $data): {{$modelBaseName}} |
||
| 24 | { |
||
| 25 | $model = new {{$modelBaseName}}((array) $data); |
||
| 26 | @if(in_array("slug",$columns->pluck('name')->toArray()) && in_array("name",$columns->pluck('name')->toArray())) |
||
| 27 | $model->slug = Str::slug($model->name); |
||
| 28 | @elseif(in_array("slug",$columns->pluck('name')->toArray()) && in_array("display_name",$columns->pluck('name')->toArray())) |
||
| 29 | $model->slug = Str::slug($model->name); |
||
| 30 | @elseif(in_array("slug",$columns->pluck('name')->toArray()) && in_array("title",$columns->pluck('name')->toArray())) |
||
| 31 | $model->slug = Str::slug($model->title); |
||
| 32 | @endif |
||
| 33 | // Save Relationships |
||
| 34 | @if (count($relations)) |
||
| 35 | @if (isset($relations['belongsTo']) && count($relations['belongsTo'])){{PHP_EOL}} |
||
| 36 | @foreach($relations["belongsTo"] as $relation) |
||
| 37 | if (isset($data->{{$relation["relationship_variable"]}})) { |
||
| 38 | $model->{{$relation['function_name']}}() |
||
| 39 | ->associate($data->{{$relation["relationship_variable"]}}->{{$relation['owner_key']}}); |
||
| 40 | } |
||
| 41 | @endforeach |
||
| 42 | @endif |
||
| 43 | @endif{{PHP_EOL}} |
||
| 44 | $model->saveOrFail(); |
||
| 45 | return $model; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function show(Request $request): {{$modelBaseName}} { |
||
| 49 | //Fetch relationships |
||
| 50 | @if (count($relations)) |
||
| 51 | @if (isset($relations['belongsTo']) && count($relations['belongsTo'])) |
||
| 52 | @php $parents = $relations['belongsTo']->pluck("function_name")->toArray(); @endphp |
||
| 53 | $this->model->load([ |
||
| 54 | @foreach($parents as $parent) |
||
| 55 | '{{$parent}}', |
||
| 56 | @endforeach |
||
| 57 | ]); |
||
| 58 | @endif |
||
| 59 | @endif |
||
| 60 | return $this->model; |
||
| 61 | } |
||
| 62 | public function update(object $data): {{$modelBaseName}} |
||
| 63 | { |
||
| 64 | $this->model->update((array) $data); |
||
| 65 | @if(in_array("slug",$columns->pluck('name')->toArray()) && in_array("name",$columns->pluck('name')->toArray())) |
||
| 66 | $this->model->slug = Str::slug($this->model->name); |
||
| 67 | @elseif(in_array("slug",$columns->pluck('name')->toArray()) && in_array("display_name",$columns->pluck('name')->toArray())) |
||
| 68 | $this->model->slug = Str::slug($this->model->display_name); |
||
| 69 | @elseif(in_array("slug",$columns->pluck('name')->toArray()) && in_array("title",$columns->pluck('name')->toArray())) |
||
| 70 | $this->model->slug = Str::slug($this->model->title); |
||
| 71 | @endif |
||
| 72 | |||
| 73 | // Save Relationships |
||
| 74 | @if (count($relations)) |
||
| 75 | @if (isset($relations['belongsTo']) && count($relations['belongsTo'])) |
||
| 76 | @foreach($relations["belongsTo"] as $relation){{PHP_EOL}} |
||
| 77 | if (isset($data->{{$relation["relationship_variable"]}})) { |
||
| 78 | $this->model->{{$relation['function_name']}}() |
||
| 79 | ->associate($data->{{$relation["relationship_variable"]}}->{{$relation['owner_key']}}); |
||
| 80 | } |
||
| 81 | @endforeach |
||
| 82 | @endif |
||
| 83 | @endif{{PHP_EOL}} |
||
| 84 | $this->model->saveOrFail(); |
||
| 85 | return $this->model; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function destroy(): bool |
||
| 89 | { |
||
| 90 | return !!$this->model->delete(); |
||
| 91 | } |
||
| 92 | public static function dtColumns() { |
||
| 93 | $columns = [ |
||
| 94 | @foreach($columnsToQuery as $col) |
||
| 95 | @if($col ==='id') |
||
| 96 | Column::make('{{$col}}')->title('ID')->className('all text-right'), |
||
| 97 | @elseif($col==='name'||$col==='title') |
||
| 98 | Column::make("{{$col}}")->className('all'), |
||
| 99 | @elseif($col==='created_at'|| $col==='updated_at') |
||
| 100 | Column::make("{{$col}}")->className('min-tv'), |
||
| 101 | @else |
||
| 102 | Column::make("{{$col}}")->className('min-desktop-lg'), |
||
| 103 | @endif |
||
| 104 | @endforeach |
||
| 105 | Column::make('actions')->className('min-desktop text-right')->orderable(false)->searchable(false), |
||
| 106 | ]; |
||
| 107 | return $columns; |
||
| 108 | } |
||
| 109 | public static function dt($query) { |
||
| 110 | return DataTables::of($query) |
||
| 111 | ->editColumn('actions', function ({{$modelBaseName}} $model) { |
||
| 112 | $actions = ''; |
||
| 113 | if (\Auth::user()->can('view',$model)) $actions .= '<button class="bg-primary hover:bg-primary-600 p-2 px-3 focus:ring-0 focus:outline-none text-white action-button" title="View Details" data-action="show-model" data-tag="button" data-id="'.$model->id.'"><i class="fas fa-eye"></i></button>'; |
||
| 114 | if (\Auth::user()->can('update',$model)) $actions .= '<button class="bg-secondary hover:bg-secondary-600 p-2 px-3 focus:ring-0 focus:outline-none action-button" title="Edit Record" data-action="edit-model" data-tag="button" data-id="'.$model->id.'"><i class="fas fa-edit"></i></button>'; |
||
| 115 | if (\Auth::user()->can('delete',$model)) $actions .= '<button class="bg-danger hover:bg-danger-600 p-2 px-3 text-white focus:ring-0 focus:outline-none action-button" title="Delete Record" data-action="delete-model" data-tag="button" data-id="'.$model->id.'"><i class="fas fa-trash"></i></button>'; |
||
| 116 | return "<div class='gap-x-1 flex w-full justify-end'>".$actions."</div>"; |
||
| 117 | }) |
||
| 118 | ->rawColumns(['actions']) |
||
| 119 | ->make(); |
||
| 120 | } |
||
| 121 | public static function seedPermissions(array $perms, array $roleNames = ["administrator"],$guard=null) { |
||
| 122 | if (!$guard) { |
||
| 123 | $guard = config('auth.defaults.guard'); |
||
| 124 | } |
||
| 125 | $perms = collect($perms); |
||
| 126 | $permissions = $perms->map(function ($permission) use($guard) { |
||
| 127 | return [ |
||
| 128 | 'name' => $permission, |
||
| 129 | 'title' => Str::title(str_replace("-"," ",implode(" ",explode(".",$permission)))), |
||
| 130 | 'guard_name' => $guard, |
||
| 131 | 'created_at' => now(), |
||
| 132 | 'updated_at' => now(), |
||
| 133 | ]; |
||
| 134 | })->toArray(); |
||
| 135 | |||
| 136 | $roles = collect($roleNames)->map(function($role) use ($perms, $guard) { |
||
| 137 | return [ |
||
| 138 | 'name' => $role, |
||
| 139 | 'title' => str_replace("-"," ", Str::title($role)), |
||
| 140 | 'guard_name' => $guard, |
||
| 141 | 'permissions' => $perms, |
||
| 142 | ]; |
||
| 143 | }); |
||
| 144 | |||
| 145 | $tableNames = config('permission.table_names', [ |
||
| 146 | 'roles' => 'roles', |
||
| 147 | 'permissions' => 'permissions', |
||
| 148 | 'model_has_permissions' => 'model_has_permissions', |
||
| 149 | 'model_has_roles' => 'model_has_roles', |
||
| 150 | 'role_has_permissions' => 'role_has_permissions', |
||
| 151 | ]); |
||
| 152 | |||
| 153 | DB::transaction(function () use($tableNames, $permissions, $roles) { |
||
| 154 | foreach ($permissions as $permission) { |
||
| 155 | $permissionItem = DB::table($tableNames['permissions'])->where([ |
||
| 156 | 'name' => $permission['name'], |
||
| 157 | 'guard_name' => $permission['guard_name'] |
||
| 158 | ])->first(); |
||
| 159 | if ($permissionItem === null) { |
||
| 160 | DB::table($tableNames['permissions'])->insert($permission); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | foreach ($roles as $role) { |
||
| 165 | $permissions = $role['permissions']; |
||
| 166 | unset($role['permissions']); |
||
| 167 | |||
| 168 | $roleItem = DB::table($tableNames['roles'])->where([ |
||
| 169 | 'name' => $role['name'], |
||
| 170 | 'guard_name' => $role['guard_name'] |
||
| 171 | ])->first(); |
||
| 172 | if ($roleItem !== null) { |
||
| 173 | $roleId = $roleItem->id; |
||
| 174 | |||
| 175 | $permissionItems = DB::table($tableNames['permissions'])->whereIn('name', $permissions)->where( |
||
| 176 | 'guard_name', |
||
| 177 | $role['guard_name'] |
||
| 178 | )->get(); |
||
| 179 | foreach ($permissionItems as $permissionItem) { |
||
| 180 | $roleHasPermissionData = [ |
||
| 181 | 'permission_id' => $permissionItem->id, |
||
| 182 | 'role_id' => $roleId |
||
| 183 | ]; |
||
| 184 | $roleHasPermissionItem = DB::table($tableNames['role_has_permissions'])->where($roleHasPermissionData)->first(); |
||
| 185 | if ($roleHasPermissionItem === null) { |
||
| 186 | DB::table($tableNames['role_has_permissions'])->insert($roleHasPermissionData); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | }); |
||
| 192 | app()['cache']->forget(config('permission.cache.key')); |
||
| 193 | } |
||
| 194 | } |
||
| 195 |