Reputation: 69
I'm trying to delete and update records in my SQLite Database. Can someone tell me what's wrong with my code? Functions seem to be fine, it's only my routes i'm having problems with.
I've tried many different methods, however i'm still having issues.
//update route
Route::post('update_item_action', function (Request $request) {
$form_content = $request->all();
$update = updatePost($form_content['id'], $form_content['description']);
return view(url("items.update_item"))->with('update', $update);
});
//delete route
Route::get('delete_item/{id}', function($id) {
$id = deletePost($id);
return redirect(url(""));
});
//delete function
function deletePost ($id) {
$sql = "delete from item where id = ?";
DB::delete($sql, array($id));
}
//update post
function updatePost ($id, $message) {
$sql = "update item set message = ? where id = ?";
DB::update($sql, array($message));
}
//html on update page
<form method="post" action="{{url("update_item_action")}}">
{{csrf_field()}}
<input type="hidden" name="id" value="{{$item->id}}">
<p>
<label> Description </label>
<input type="text" name="description" value="{{$item->description}}">
</p>
<input type="submit" value="Submit" class="submit">
</form>
I'm simply trying to update my description, and delete the whole post.
Error for updating:
Call to undefined method Illuminate\Support\Facades\Request::all()
Thank you
Upvotes: 0
Views: 286
Reputation: 1353
The issue within your update method is you are requesting two arguments to be passed to function but you aren't defining them in route.
The route should look like this:
Route::post('update_item_action/{id}/{message}', function($id, $message) {
$update = updatePost($id, $message);
return redirect(url("")) ->with ('update', $update);
});
If you want to retrieve the content of form, you should use Request
class instance as function's argument:
use Illuminate\Http\Request; // DO NOT USE Request FACADE
Route::post('update_item_action', function (Request $request) {
$form_content = $request->all();
$update = updatePost($form_content['id'], $form_content['description']);
return redirect(url(""))->with('update', $update);
});
Take a look at Laravel's request docs on how to retrieve the content of the form or of the content of all requests sent to your server.
Update
Regarding delete route,
<a class="delete" href="{{url("delete_item/$item->id")}}">Delete Item </a>
will send a GET request to server, but you are explicitly requesting that delete have to be done via POST request.
In this scenario you have two options: change the route definition to
Route::get('delete_item/{id}', function($id) {
/*etc..*/
});`
or making an async call to server using POST method, i.e. using jQuery, using this solution, or any other you can find googling a bit.
Upvotes: 1