Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete not working on minimal install of laravel-code-generator #181

Closed
levyra opened this issue Feb 2, 2022 · 4 comments
Closed

Delete not working on minimal install of laravel-code-generator #181

levyra opened this issue Feb 2, 2022 · 4 comments

Comments

@levyra
Copy link

levyra commented Feb 2, 2022

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>
@yield('content')

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(&quot;Click Ok to delete Color.&quot;)">
                                        <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; } }
@MikeAlhayek
Copy link
Collaborator

What happens when you try to delete a model? Do you get an error?

you may want to look at Laravel’s logs for more clues.

@levyra
Copy link
Author

levyra commented Feb 2, 2022

I forgot to include the site: https://rlevy.assessmentengineering.com/colors

Get no error: it returns to https://rlevy.assessmentengineering.com/colors/color/2

LOGS
/storage/logs$ sudo truncate -s 0 laravel.log
No new logs created when

  1. https://rlevy.assessmentengineering.com
  2. https://rlevy.assessmentengineering.com/colors
  3. Deleted color

@SiL3NC3
Copy link

SiL3NC3 commented Feb 20, 2022

Probably, because just a js call is being done, and not action triggered on the database?!

FILE: resources/laravel-code-generator/templates/default/index.blade.stub

Line 66:
... onclick="return confirm(&quot;[% confirm_delete %]&quot;)" ...

@knkhayam
Copy link
Contributor

knkhayam commented Mar 2, 2022

Hello there,
The bug was due to an error in the routes.
I have fixed and created a pull request #182
Merging this will fix the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants