awais-vteams / laravel-crud-generator

Laravel CRUD Generator
https://packagist.org/packages/ibex/crud-generator
MIT License
593 stars 117 forks source link

Controller signatures should use Models instead of IDs #24

Closed etopian closed 6 months ago

etopian commented 6 months ago

The result of using IDs instead of Model objects on Controller is that some routes are registered like for instance {table_name}/list that should return not found and will if you used Models as signatures with the recommended route.

For instance

    public function show(Server $server)
    {
        return view('server.show', compact('server'));
    }

Instead of

    public function show($id)
    {
        $server = Server::find($id);

        return view('server.show', compact('server'));
    }

You can also add the Request object to the controller

    /**
     * Display the specified resource.
     */
    public function show(Request $request, Server $server)
    {
        return view('server.show', compact('server'));
    }

These are auto-loaded by Laravel.

awais-vteams commented 6 months ago

@etopian Actually for some reason where users add custom routes it will not work because the routes param name should be equal to the name of the variable defined in controller arguments.

but I have improved within the next version 2.x Please look into the new version that will be relasing soon with Tailwind, Livewire, React, etc https://github.com/awais-vteams/laravel-crud-generator/tree/2.x

etopian commented 6 months ago

Sorry don't understand what you are saying.

I am saying when you add this route to the web.php routes file as recommended, if you use id instead of Object in Laravel it will allow for routes that match the $id variable which could be text as well as numbers. If you use Objects then it will attempt to load the object using the ID provided and if that does not work it gives a page not found:

Route::resource('servers', ServerController::class);

awais-vteams commented 6 months ago

I got your point already and you are correct too. It will updated in the next version. Thannks