Skip to content
Advertisement

i want to route from a datatable with action buttons using the url method, but on successful data fetch, buttons routes to 404 page

*VendorController*- *this is the datatable function*

    public function datatable(){
        $data = Vendor::all();
        try {
            return DataTables::of($data)->addColumn('action', function ($data) {
                $url_edit = url('master/vendor/'.$data->id.'/edit');
                $url = url('master/vendor/'.$data->id.'/');
                $view = "<a class='btn btn-action btn-primary' href='$url' title='View'><i class='nav-icon fas fa-eye'></i></a>";
                $edit = "<a class='btn btn-action btn-warning' href='$url_edit' title='View'><i class='nav-icon fas fa-edit'></i></a>";
                $delete = "<button data-url='" . $url . "' onclick='deleteData(this)' class='btn btn-action btn-danger' title='delete'><i class='nav-icon fas fa-trash'></i></button>";

                return $view."".$edit."".$delete;
            })->editColumn('address', function ($data) {
                return str_replace("rn", ',', $data->address);
            })->editColumn('phone', function ($data) {
                return str_replace("rn", ',', $data->phone);
            })->rawColumns(['action'])->editColumn('id', 'ID:{{$id}}')->make(true);
        } catch (Exception $e) {
        }
    }


*web.php* - *here i am making use of resource for my routing*

 
Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');
Route::resource('/master/vendor/', 'AppHttpControllersMasterVendorController');
Route::get('vendor/datatable', [AppHttpControllersMasterVendorController::class, 'datatable'])->name('vendor/datatable');

*The edit function*


 public function edit($id)
    {
        $data = Vendor::where('id', $id)->where('active', '!=', 2)->get();
        if ($data->count() > 0){
            return view('Vendor.update', compact('data'));
        }
    }

*DataTable Script* - *here's the datatable script*
   <script>
        $(function () {
             $('#example1').DataTable({
                responsive: true,
                processing:true,
                pagingType:'full_numbers',
                stateSave:false,
                scrollY:true,
                scrollX:true,
                ajax:"{{url('vendor/datatable')}}",
                order:[0, 'desc'],
                columns:[
                    {data:'name', name:'name'},
                    {data:'address', name:'address'},
                    {data:'cp', name:'cp'},
                    {data:'phone', name:'phone'},
                    {data:'active',
                        render:function(data){
                        if(data=='1'){
                            return '<span class="badge badge-success">Active</span>'
                        }
                        if(data=='2'){
                            return '<span class="badge badge-warning">Inactive</span>'
                        }
                    }
                    },
                     {data:'action', name:'action', searchable:false, sortable:false}
                ]
            });
        });
    </script>

Please i’ll be online for anything else that i might need to show Please i’ll be online for anything else that i might need to show Please i’ll be online for anything else that i might need to show Please i’ll be online for anything else that i might need to show Please i’ll be online for anything else that i might need to show Please i’ll be online for anything else that i might need to show

Advertisement

Answer

Issue: The route generated by resource routing does not contain a mandatory route parameter name, it was an {} empty which should have been {vendor} like this.

Route::resource('/master/vendor/', 'AppHttpControllersMasterVendorController');

Output:
+--------+-----------+-----------------------+---------+-----------------------------------------+------------+
| Domain | Method    | URI                   | Name    | Action                                  | Middleware |
+--------+-----------+-----------------------+---------+-----------------------------------------+------------+
|        | GET|HEAD  | master/vendor         | index   | AppHttpControllersController@index   | web        |
|        | POST      | master/vendor         | store   | AppHttpControllersController@store   | web        |
|        | GET|HEAD  | master/vendor/create  | create  | AppHttpControllersController@create  | web        |
|        | GET|HEAD  | master/vendor/{}      | show    | AppHttpControllersController@show    | web        |
|        | PUT|PATCH | master/vendor/{}      | update  | AppHttpControllersController@update  | web        |
|        | DELETE    | master/vendor/{}      | destroy | AppHttpControllersController@destroy | web        |
|        | GET|HEAD  | master/vendor/{}/edit | edit    | AppHttpControllersController@edit    | web        |
+--------+-----------+-----------------------+---------+-----------------------------------------+------------+

See all {} in URI which was causing a 404 when you generate a route like master/vendor/1/edit to edit resource #1.

The issue was due to trailing slash / in resource routing /master/vendor/. In resource routing the last variable is place inside curly braces to make it a parameter name. But in this case laravel thinks that it is an empty value after slash causing {} this in routes.

Remove trailing slash and the output would be.

Route::resource('/master/vendor', 'AppHttpControllersMasterVendorController');

Output:
+--------+-----------+-----------------------------+----------------+-----------------------------------------+------------+
| Domain | Method    | URI                         | Name           | Action                                  | Middleware |
+--------+-----------+-----------------------------+----------------+-----------------------------------------+------------+
|        | GET|HEAD  | master/vendor               | vendor.index   | AppHttpControllersController@index   | web        |
|        | POST      | master/vendor               | vendor.store   | AppHttpControllersController@store   | web        |
|        | GET|HEAD  | master/vendor/create        | vendor.create  | AppHttpControllersController@create  | web        |
|        | GET|HEAD  | master/vendor/{vendor}      | vendor.show    | AppHttpControllersController@show    | web        |
|        | PUT|PATCH | master/vendor/{vendor}      | vendor.update  | AppHttpControllersController@update  | web        |
|        | DELETE    | master/vendor/{vendor}      | vendor.destroy | AppHttpControllersController@destroy | web        |
|        | GET|HEAD  | master/vendor/{vendor}/edit | vendor.edit    | AppHttpControllersController@edit    | web        |
+--------+-----------+-----------------------------+----------------+-----------------------------------------+------------+

Old Solution: Due to mandatory id parameter it is causing route not found. Created routes manually

Route::get('/master/vendor/{id}/edit', 'AppHttpControllersMasterVendorController@edit');
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement