Description
Environment:
- Laravel-Code-Generator Version: v2.4.7
- Laravel Version: v8.82.0
Description:
Vanilla build: Only Laravel/Crestapps/Mysql
Steps/Commands To Reproduce:
MYSQL:
CREATE TABLE colors
(
id
int(11) NOT NULL AUTO_INCREMENT,
color
varchar(45) DEFAULT NULL,
is_primary
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
app.blade.php
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"> </script> <title>{{ config('app.name', 'Laravel') }}</title>artisan
php artisan create:scaffold Color --table-exists
Use site to do CRUD
Delete does not delete
Content Of The Resource-File:
ROUTE
Route::group([
'prefix' => 'colors',
], function () {
Route::get('/', [ColorsController::class, 'index'])
->name('colors.color.index');
Route::get('/create', [ColorsController::class, 'create'])
->name('colors.color.create');
Route::get('/show/{color}',[ColorsController::class, 'show'])
->name('colors.color.show')->where('id', '[0-9]+');
Route::get('/{color}/edit',[ColorsController::class, 'edit'])
->name('colors.color.edit')->where('id', '[0-9]+');
Route::post('/', [ColorsController::class, 'store'])
->name('colors.color.store');
Route::put('color/{color}', [ColorsController::class, 'update'])
->name('colors.color.update')->where('id', '[0-9]+');
Route::delete('/color/{color}',[ColorsController::class, 'index'])
->name('colors.color.destroy')->where('id', '[0-9]+');
});
INDEX.BLADE.PHP form element
<form method="POST" action="{!! route('colors.color.destroy', $color->id) !!}" accept-charset="UTF-8">
<input name="_method" value="DELETE" type="hidden">
{{ csrf_field() }}
<div class="btn-group btn-group-xs pull-right" role="group">
<a href="{{ route('colors.color.show', $color->id ) }}" class="btn btn-info" title="Show Color">
<span class="glyphicon glyphicon-open" aria-hidden="true"></span>
</a>
<a href="{{ route('colors.color.edit', $color->id ) }}" class="btn btn-primary" title="Edit Color">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>
<button type="submit" class="btn btn-danger" title="Delete Color" onclick="return confirm("Click Ok to delete Color.")">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</button>
</div>
</form>
COLORS CONTROLLER
getData($request); Color::create($data); return redirect()->route('colors.color.index') ->with('success_message', 'Color was successfully added.'); } catch (Exception $exception) { return back()->withInput() ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']); } } /** * Display the specified color. * * @param int $id * * @return Illuminate\View\View */ public function show($id) { $color = Color::findOrFail($id); return view('colors.show', compact('color')); } /** * Show the form for editing the specified color. * * @param int $id * * @return Illuminate\View\View */ public function edit($id) { $color = Color::findOrFail($id); return view('colors.edit', compact('color')); } /** * Update the specified color in the storage. * * @param int $id * @param Illuminate\Http\Request $request * * @return Illuminate\Http\RedirectResponse | Illuminate\Routing\Redirector */ public function update($id, Request $request) { try { $data = $this->getData($request); $color = Color::findOrFail($id); $color->update($data); return redirect()->route('colors.color.index') ->with('success_message', 'Color was successfully updated.'); } catch (Exception $exception) { return back()->withInput() ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']); } } /** * Remove the specified color from the storage. * * @param int $id * * @return Illuminate\Http\RedirectResponse | Illuminate\Routing\Redirector */ public function destroy($id) { try { $color = Color::findOrFail($id); $color->delete(); return redirect()->route('colors.color.index') ->with('success_message', 'Color was successfully deleted.'); } catch (Exception $exception) { return back()->withInput() ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']); } } /** * Get the request's data from the request. * * @param Illuminate\Http\Request\Request $request * @return array */ protected function getData(Request $request) { $rules = [ 'color' => 'nullable|string|min:0|max:45', ]; $data = $request->validate($rules); return $data; } }