简体   繁体   中英

delete data from database using model in laravel 5

I am trying to delete data from my table employes by getting the id of the data and transferring it by using get route but i get the following error

ModelNotFoundException in Builder.php line 125: No query results for model [App\\employe].

her is my route

Route::get('/delete/{employe}', 'EmployeController@delete');
Route::post('/delete', 'EmployeController@handleDelete');

and i have model called employe to access my table

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class employe extends Model {

    protected $fillable = array('fname','lname','age','sex','phone','email','houseno','city','kebele','state','username','password','workposition','salary','bankaccount','bankname','contactid','employedate');


}

part of my controller for deleting

    public function delete(employe $employe)
    {
    // Show delete confirmation page.
    return View('deleteemploye', compact('employe'));
    }

    public function handleDelete()
    {


    $employe = employe::findOrFail(Input::get('employe'));
    $employe->delete();

    return Redirect::action('EmployeController@index');

}

her is my view for deleting file

@extends('app')

@section('content')

    <div class="page-header">
        <h2>Delete {{$employe->fname}} Are you sure?</h2>
    </div>
    <form action="{{ action('EmployeController@handleDelete') }}" method="post" role="form">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="hidden" name="employe"  id="employe" value="{{ $employe->id }}" />
        <input type="submit" class="btn btn-default" value="Yes" />
        <a href="{{ action('EmployeController@index') }}" class="btn btn-default">No</a>
    </form>
@stop  

Looks like laravel cant find the model with the primary key you are passing. Check the database and see if its there. If it is check the deleted_at field. If it has a date in it, it means it is already deleted. You can still find it by using withTrashed()

$employe = employe::withTrashed()->findOrFail(Input::get('employe'));

and if you have soft deleting enabled for this model, you can completely remove the given row by running

$employe = employe::withTrashed()->findOrFail(Input::get('employe'))->forceDelete();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM