Search code examples
phpsqllaravelsql-deleteinformation-schema

delete mysql with laravel


I need to delete information from the database through a submit post. But it updates and does not delete information.

Route:

Route::get('/home', 'HomeController@index')->name('home');
Route::post('/home/linha/insert', 'HomeController@insert')->name('home');
Route::post('/home/linha/remove','HomeController@deletar');

Controller:

    public function deletar(Request $request)
{


    $id = $request->input('id');

     DB::delete('delete from linha where id = :id' ,['id' => $id]);


      return redirect('home');


}

View:

<div class="col-md-9" style="margin-top:20px;">
    <div class="col-md-9"><?php echo "$linha->msg"; ?></div>
    @if($linha->iduser == auth()->user()->id)
    <div class="col-md-3"><form action="/home/linha/remove" method="post">
        {{ csrf_field() }}
        <input type="hidden" name="id" id="id" value="{{$linha->id}}">
       <button type="submit" class="btn btn-danger btn-sm">
          <span class="glyphicon glyphicon-remove"></span> Remover 
        </button>
        </form>
        </div>@endif
</div>

Solution

  • Try This: It was shown in Laravel Documentation as well.

    $deleted = DB::delete('delete from linha where id = ?',[$id]);
    

    Here is link of Documentation ( https://laravel.com/docs/5.7/database ).

    Or You Can Use laravel eloquent "linha"

    $request->YourModel()->findOrFail($id)->delete();