i´m trying to do one button to remove one data from my database. I´m doing a form
public function datatable()
{
$data = User::all();
return Datatables::of($data)
->addColumn('action', function($user){
$btn = '<div >
<a href='.route('options.users.show', $user->id).' >
<i ></i>
</a>';
$btn .= '<form action="{{ route(\'options.users\','.$user->id.') }}" method="POST">
'.csrf_field().''.method_field("DELETE").'
<button type="submit"
onclick="return confirm(\'Are You Sure Want to Delete?\')"><i ></i></a>
</form>
</div>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
But when click in my button return 404 error and my URL it´s this....
options/{{ route('options.users',1) }}
i don´t know that i´m doing wrong
/** UPDATE */
Route::group(['prefix'=>'options','as'=>'options.'], function(){
Route::get('/opciones', 'OpcionesSistema@index')->name('index');
Route::post('/cambiarFotoPerfil', 'OpcionesSistema@changeImages')->name('cambiarFotoPerfil');
Route::post('/cambiarDatosLogin', 'OpcionesSistema@changeLoginData')->name('cambiarDatosLogin');
Route::post('/cambiarNombreSistema', 'OpcionesSistema@changeNameSystem')->name('changeNameSystem');
Route::resource('roles', 'RolesPermisos');
Route::get('users/datatable', 'UserController@datatable')->name('datatable');
Route::resource('users', 'UserController');
});
/** UPDATE 2 */
controller:
/**
* FUNCTION TO RETURN DATATABLE
*/
public function datatable()
{
$data = User::all();
return Datatables::of($data)
->addColumn('action', function($user){
return view('layouts.datatableActions', ['user' => $user]);
})
->rawColumns(['action'])
->make(true);
}
layouts/view
@props('user')
<div >
<a href='{{ route('options.users.show', $user) }}' >
<i ></i>
</a>
<form action="{{ route('options.users', $user) }}" method="POST">
@method('DELETE')
@csrf
<input type="submit" />
</form>
</div>
My new error it´s: Message:\n\nUndefined variable $attributes (View: C:\xampp\htdocs\clinica\resources\views\layouts\datatableActions.blade.php)"
CodePudding user response:
Try this:
Your view.blade.php
@props(['user'])
<form action="{{ route('options.users', $user) }}" method="POST">
@method('DELETE')
@csrf
<input type="submit" />
</form>
your controller:
public function datatable()
{
$data = User::all();
return Datatables::of($data)
->addColumn('action', function($user){
return view('view', ['user' => $user]);
});
and in your web.php
:
Route::delete('users', 'UserController');
CodePudding user response:
You are inside of a PHP block, which means that Blade syntax will not work. Just remove that, and concatenate the route just like you have the rest of the PHP functions, and just like the line before it.
$btn .= '<form action="'.route('options.users',$user->id).'" method="POST">
'.csrf_field().''.method_field("DELETE").'
<button type="submit"
onclick="return confirm(\'Are You Sure Want to Delete?\')"><i ></i></a>
</form>
</div>';