Delete my database record using routes in Laravel? - php

I'm using Laravel and I'm trying to achieve a delete function that takes care of deleting the selected task the user made.
I was wondering how can I achieve this using routes?
<button class="btn btn-outline-light">Edit</button>
<button class="btn btn-outline-light">Delete</button>
At this moment my delete route takes me to the same page as what the edit route does.
My delete function:
public function destroy(Task $task)
{
$task->delete();
return redirect('/tasks')->with('success', 'Task removed');
}
I got a working delete function using the Laravel forms:
{!! Form::open(['action' => ['TasksController#destroy', $task->id], 'method' => 'POST', 'class'=> 'float-right']) !!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-outline-danger'])}}
{!! Form::close() !!}
Is it worth to consider another method of taking care of my deletes or should I stick with using the Laravel forms?

That's because of CSRF protection. You may think using an ajax request.
You can find an example in this post.
How to delete record in laravel 5.3 using ajax request?

When you press the delete link, you'll be basically making a GET request to the tasks.destroy route, which is not what you want. You can either create a form and submit it on click (the laravel logout btn does something similar)
<a href="#"
onclick="event.preventDefault(); document.getElementById('task-destroy-form').submit();">
Delete task
</a>
<form id="task-destroy-form" action="{{ route("tasks.destroy", ["id" => $task->id]) }}" method="POST" style="display: none;">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
</form>
or make an ajax call that will send the id and CSRF parameters to the route.

Related

Missing required parameters for [Route: admin.event.destroy] [URI: admin/evenment/event/{id}]

Hey guys i have an error when i submit the delete it says that i'm not sending the id to the route i think,
that's the code to send the id to the route (and sorry for my english),
<form id="del_type" action="{{ route('admin.event.destroy',$event->id)}}" method="post">
{!! method_field('delete') !!}
{{ csrf_field() }}
<button class="btn btn-danger" type="submit" id="del_id">Supprimer</button>
it's wierd cause when i submit it's deleting the element from the database but with this error and in the URI i can see the id
take a look at the routes (All route names are prefixed with 'admin.')
Route::delete('evenment/event/{id}','EventController#destroy')->name('event.destroy');
change route to
admin.event.destroy
Change action to this and try
{{ route('admin.event.destroy', ['id' => $event->id])}}

DELETE request on a link in laravel 5.4

I am trying to hit a route I made to make a delete HTTP request in laravel view when user clicks on 'Delete' button, but it won't work. I've read it should be done with forms in laravel.
Here is my code:
<form action="/admin/pages/delete/{{ $section->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-sm" type="submit">Delete</button>
</form>
What is a proper way to handle this?
It shows me an error in the console, Bootbox: 'please specify a message' whenever I click on the button.
Route definition inside admin group:
Route::delete('/pages/delete/{id}', 'PagesController#delete')->name('pages.delete');
I believe you are missing the csrf token in the form.
You can add
{{ csrf_field() }}
just after your form starts.
Visit this link for knowing more about csrf
You must add the the CSRF Field because all form submission must past through the VerifyCsrfToken middleware before the request be procede by the controller
{{ csrf_field() }} // add this before or after the {{ method_field() }}

Laravel, get data from controller for display purposes

I'd like to display data calculated in controller in a blade view based on data provided by a user in form . For now I'm doing this by:
//View xyz.blade.php
<div class="card">
<div class="card-header">Add link</div>
<div class="card-block">
{{ Form::open(array('url' => 'getSimilar', 'method' => 'GET')) }}
{{ Form::token() }}
<div class="form-group">
{{ Form::label('url', 'url:') }}
{{ Form::text('url', '') }}
</div>
{{ Form::submit('Get') }}
{{ Form::close() }}
</div>
#if( ! empty($similar))
<div class="card-block">
#foreach ($similar as $item)
{{$item->title}} <br/>
#endforeach
</div>
#endif
</div>
//Controller
public function getSimilar(){
..
return View('xyz', ['similar' => $found]);
}
The above code works as expected. I can provide a url and after clicking "Get" I can see a list of found items below the form. However, something tells me that this is not the right way to do this since the entire page will refresh. Am I right (and so the above code is bad)? If so, is there any build in feature to display fetched data without refreshing? I searched on the official Laravel-form page but I did not find anything.
As far as I could understand your question.
The code seems to be ok, but the logic you've created may not be possible without a page refresh, because the user interaction depends the server response which requires a new reload.
You can craft another interactive action without a page refresh using an AJAX so when the user clicks the button he gets the result from the server you then display the results in page. Because the AJAX Request/Response happens behind the scenes for the user it gives an impression the page didn't reload which may be what you want.

Laravel 5.4 "MethodNotAllowedHttpException" (Basic Task List)

i was trying to follow this tutorial on Laravel, even though most of the code & directories there were outdated, i managed to make the "Add Task" and also The Form to show up after a few errors.
now the problem, the delete button.
when i click on it, it shows a "MethodNotAllowedHttpException".
i changed the source code to match the newest version of Laravel.
my form (current version) :
<form action="{{ url('/task/'.$task->id) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
my route :
Route::delete('/task/{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
i've been trying to fix this for 4 hours now, changing the methods of my route and form; but to no avail.
this is my first question on this site, sorry if there's something wrong in this question.
thanks~
edit:
to further help the effort, here's the complete error log
Error log, in Google Chrome
Change
<form action="{{ url('task/'.$task->id) }}" method="DELETE">
to
<form action="{{ url('task/'.$task->id) }}" method="POST">
Because form method DELETE does not exist, Laravel just "overwrites" this method with the hidden input "method" (you can place this input using "{{ method_field('DELETE') }}").
Form dosent support method Delete or Put ... It support only get and post methods, if You want implement delete in laravel this article will help you
link
Answer :
Turns out the problem lies in the Database-table itself, let me explain :
i was referring to a 'tasks' table that is referred as 'task' in code (no problem)
BUT, i was referring to a column called "ID" in my table as "id" in my code, creating the error (a rookie mistake).
thanks to #Autista_z for the pointer, and everyone else below for the guidance!

Laravel Delete Items within Edit View

I am using Laravel 5 and I need to call a destroy Method inside my edit view.
All examples I found cover this separated with an index view and two buttons (one for editing and one for deleting).
Can somebody give me a hint to call a destroy Method inside an edit view?
You can put this code anywhere, not just in index view. Just create a link and use get route or use destroy route with form button to send a request:
{!! Form::open(['method' => 'Delete', 'route' => ['someroute.destroy', $id]]) !!}
<button type="submit">Delete</button>
{!! Form::close() !!}
Put this in the show view of the item
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button class="btn btn-danger">Delete Post</button>
</form>

Categories