6

Recently I've started with Laravel 5.2 and I'm trying to make delete button which will delete row from database. Very basic and trivial but seems I can't make it.

I'm following documentation for delete: https://laravel.com/docs/5.2/queries#deletes

And I have made this. My route:

Route::post('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

Button in the view

{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}

and the controller

public function destroy(Request $request){

    $report = $request['report_id'];      

    Report::find($report);

    $report->delete();        
    $request->session()->flash('alert-success', ' Report is deleted successfully.');

    return redirect()->route('admin.flags');
}

I've tried solutions from other threads but I always got error:

MethodNotAllowedHttpException in compiled.php line 8936:

New error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reports.id' in 'where clause' (SQL: select * from `reports` where `reports`.`id` is null limit 1

Why is searching for id instead of report_id?

UPDATE:

button

{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}

Controller

public function destroy(Request $request){

    $report = $request['report_id'];      

    dd( $request->input('delete'));

    Report::where('report_id', $report)->first();

    $report->delete();        
    $request->session()->flash('alert-success', ' Report is deleted successfully.');

    return redirect()->route('admin.flags');
}

Route

Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

Update 2: This seems to work but is it secure enough? view:

 {!! Form::open(array('route' => array('admin.flags.destroy', $flag->report_id), 'method' => 'get')) !!}
        <button type="submit">Delete</button>
 {!! Form::close() !!}</td> 

Controller

public function destroy($report_id){

  Report::destroy($report_id);
  //$request->session()->flash('alert-success', ' Report is deleted successfully.');

  return redirect()->route('admin.flags');
}

4 Answers 4

5

I think your code need to update like:

public function destroy($delete){

   $report = $delete;      

   $rsltDelRec = Report::find($report);

   $rsltDelRec->delete();        
   $request->session()->flash('alert-success', ' Report is deleted successfully.');

   return redirect()->route('admin.flags');
}

Hope this work for you!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. Got this Call to a member function delete() on string
dd($report); return "1" i.e. string
@VLS Please check my answer. I made correction for it.
2

Try this:

Controller:

  public function destroy(Report $report){

          $report->delete();

          return redirect()->route('admin.flags');

    }

Comments

1

You're creating get link but using post route. Change it to:

Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

3 Comments

use reports_id not reports.id.check your query
I'm using it.. check the function and query in my question. It is requesting for report)id..
check primary key in your model. it should be like protected $primaryKey = 'report_id'; // find() uses primary key // looks like you have $primaryKey = 'id'
1

MethodNotAllowedHttpException means that you are trying to access route with bad method. If you use Html::linkRoute then anchor is generated, but in your routes you have defined Route::post. You need to replace Route::post with Route::get. But if you want to make it safer you need to create simple form with delete button and CSRF token.

<form method="POST" action="{{ URL::route('admin.flags.destroy', {'delete' => $flag->report_id}) }}">
    {{ csrf_field() }}
    <!-- submit button -->
</form>

Why is searching for id instead of report_id?

You need to replace

Report::find($report);

with

$report = Report::where('report_id', $report)->first();

public function destroy(Request $request){

    $report = $request['report_id'];
    ....

You are trying here to access report_id in request, but in routes you named your parameter as delete

14 Comments

Hm, now Call to a member function delete() on null this
deleting a null value
change $request['report_id']; to $request['delete'];
Still same. @Bugfixer I know what is mean . just can't figure out why is null and how to fix
But someone who can send you mail, cant predict your token. They are unique and connected to your current session.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.