如何使用带有多个或不同列到ID的findOrfail删除行。 Laravel

时间:2018-04-15 04:04:13

标签: php laravel crud

我是lavravel的新手,并尝试使用laravel和一个简单的代码删除一行

<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

我有我的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');
 });

当我尝试删除一行时,收到错误:

SQLSTATE [42S22]:未找到列:1054未知列&#39; usuario.id&#39;在&#39; where子句&#39; (SQL:从usuario中选择{usuarioid = 14397755限制1)

我需要找到usuario.ident删除该行..

我真的需要和{fk_id_tid&amp; ident}如果我找到它 - &gt;删除该行..

2 个答案:

答案 0 :(得分:1)

Laravel不支持复合键,请参阅here。因此,您必须手动require 'CSV' hs = %w{ First_name Last_name email_address person_id person_number sis_username location_id } CSV.open('skyward.csv', "wb") do |csv| csv << hs CSV.foreach('asm.csv', headers: true) do { |row| << row.values_at(*hs) } CSV.foreach('google.csv', headers: true) do { |row| << row.values_at(*hs) } end 来搜索该行

where()

如果您在删除路由中也传递了Usuario::where('ident', $ident)->firstOrFail()->firstOrFail()->delete(); ,那么为外键搜索添加一个fk_id_tid,如

where()

答案 1 :(得分:0)

我在尝试:

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

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

});

传递表格

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

错误..