2

Im new in lavravel and and try to delete a row using laravel and a simple code

<div class="col-md-12">
@if (count($usuarios) > 0)
<?php echo $usuarios?>
<div class="panel panel-default">
    <div class="panel-heading">
        Listado de Usuarios
    </div>
    <div class="panel-body">    
    <table class="table table-striped task-table">
        <thead>
            <th>Usuario</th>
            <th>Correo electrónico</th>
            <th>Telefono</th>
            <th>Celular</th>
            <th>Acción</th>
        </thead>    
        <tbody>
        @foreach($usuarios as $usuario)
            <tr>
                <td class="table-text">
                    <div>{{ $usuario->fname }} {{ $usuario->mname }} {{ $usuario->lname }} {{ $usuario->lname2 }}</div>
                </td>
                <td class="table-text">
                    <div>{{ $usuario->email }} </div>
                </td>
                <td class="table-text">
                    <div>{{ $usuario->telefono }} </div>
                </td>
                <td class="table-text">
                    <div>{{ $usuario->celular }} </div>
                </td>

                <td>
                    <form action="{{ url('usuario') }}/{{ $usuario->ident }}" method="POST">
                    {{ csrf_field() }}
                    {{ method_field('DELETE') }}

                    <button type="submit" class="btn btn-danger">
                        <i class="fa fa-trash"></i>Delete
                    </button>

                    </form>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    </div>
</div>
@endif

and I have my web.php

use App\Usuario;
use Illuminate\Http\Request;

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

 /*Mostrar los Usuarios*/
 Route::get('/usuarios', function () {
 $usuarios = Usuario::all();
 return view('usuarios', [ 'usuarios' =>$usuarios ]);


 });

 Route::post('/usuario', function (Request $request) {
 $validator = Validator::make($request->all(), [
    'ident' => 'required|digits_between:5,12',
    'fname' => 'required|max:20',
    'mname' => 'required|max:20',
    'lname' => 'required|max:20',
    'lname2' => 'required|max:20',
    'email' => 'email',
    'telefono' => 'min:7',
    'celular' => 'min:10',

  ]);

 if ($validator->fails()){
    return redirect('/usuarios')
        ->withErrors($validator)
        ->withInput();

 }
$usuario = new Usuario;
$usuario->fk_id_tid = $request->id_tid;
$usuario->ident = $request->ident; 
$usuario->fname = $request->fname;
$usuario->mname = $request->mname;
$usuario->lname = $request->lname;
$usuario->lname2 = $request->lname2;
$usuario->email = $request->email;
$usuario->telefono = $request->telefono;
$usuario->celular = $request->celular;

$usuario->save();

return redirect('/usuarios');
});

**Route::delete('/usuario/{ident}', function ($ident) {
Usuario::findOrFail($ident)->delete();** 

return redirect('/usuarios');
 });

When I try to delete a Row, Receive error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'usuario.id' in 'where clause' (SQL: select * from usuario where usuario.id = 14397755 limit 1)

I need to find usuario.ident for delete that row..

Well Really I need to make a composite with {fk_id_tid & ident} If I find it --> delete that row..

2 Answers2

2

Laravel doesn't support composite keys see here. So you have to manully have to do a where() to search that row

Usuario::where('ident', $ident)->firstOrFail()->firstOrFail()->delete();

If you are passing fk_id_tid too in your delete route then add one more where() for the foreign key search like

Route::delete('/usuario/{fk_id_tid}/{ident}', function ($fk_id_tid, $ident) {

  Usuario::where('fk_id_tid', $fk_id_tid)->where('ident', $ident)->firstOrFail()->delete();
  return redirect('/usuarios');

});
Vinay
  • 7,442
  • 6
  • 25
  • 48
0

Im trying:

Route::delete('/usuario/{ident}', function ($ident) {

Usuario::where('ident', $ident)->findOrFail()->delete();  
return redirect('/usuarios');

});

Passing in the form

<form action="{{ url('usuario') }}/{{ $usuario->ident }}" method="POST">
                {{ csrf_field() }}
                {{ method_field('DELETE') }}

errors..