Delete a record gave me NotFoundException in laravel 5 - php

How can I create multiple requests for the same route like below.
Route.php
Route::get('/home', 'HomeController#index');//->middleware('auth');
Route::get('/home/{$user}','HomeController#showStudent');
Route::delete('/home/{$studentId}','HomeController#deleteStudent');
the form was working fine until I have added the delete request. In my blade template I have code something like this.
home.blade.php
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
{{ csrf_field() }}
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
I believe because of the same routes it's showing NotFoundHTTPException.
On one route /home I am trying to Add, Show, Edit and Delete a record with different buttons.
Thanks in Advance.

You could add a form and use Laravel's Form Method Spoofing
<input type="hidden" name="_method" value="DELETE">
See more here...http://laravel.com/docs/master/routing#form-method-spoofing
Try as below....
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>

1) Change you route from:
Route::delete('/home/{$studentId}','HomeController#deleteStudent');
To:
Route::get('/delete/{$Id}','HomeController#deleteStudent')->name('delete');
2) change you form tag from:
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
To:
<form class="" role="form" method="get" action="route('delete', ['id' => $student->id])">

HTML forms doesn't support methods other than get and post. If you need to simulate it, include a hidden input to simulate delete:
<input name="_method" type="hidden" value="DELETE">
Then in your code, update it to:
<form class="" role="form" method="POST" action="/home/{{$student->id}}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
Reference:
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
http://laraveldaily.com/theres-no-putpatchdelete-method-or-how-to-build-a-laravel-form-manually/

Related

post route in Laravel

I have done everything the right way but my submit button doesnt do anything and I dont know why....
Here is my view
<form action="{{ route('importUser') }}" method="POST" enctype="multipart/form-data">
#csrf
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
Here is my controller
function importUser(Request $request)
{
#code...
}
and my route
Route::POST('ImportUsersFile', 'ExcelUserController#importUser')->name('importUser')->middleware('Admin');
Apparently, the flow dont get in the function import user. I tried to dd into it but nothing happend!
According to an error message you provided in a comment, try this:
php artisan key:generate
Try using url intead of route
<form action="{{ url('ImportUsersFile') }}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
And in your routes:
Route::post('ImportUsersFile', ['uses' => 'ExcelUserController#importUser', 'as' => 'importUser']);

Laravel delete item from table list / db

I am trying to delete item from a generated table of items which are from a database table.
My Route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController#destroyDevice']);
My Controller method to delete an item:
public function destroyDevice(Request $request, $deviceId = 0)
{
$device = Device::find($deviceId);
if($device)
{
$device->delete();
return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
}
else
{
return redirect()->route('index')->with('error', 'Fehler');
}
}
And my blade template:
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<td>
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</form>
If I click on the button nothing happens no error no Response, what am I doing wrong.
If I click on the third delete button the form holds this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
You can solve this by putting the form inside a td tag in that table.
Like this:
<td> <!-- <--- put these -->
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td> <!-- <--- put these -->
I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)
The parameter is case sensitive so it should be deviceID instead of deviceId
public function destroyDevice(Request $request, $deviceID = 0)
Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Laravel method not allowed exception

I can't seem to find out why does my submit give me back the MethodNotAllowedException. Shortened...here is the form:
<form role="form" id="tryitForm" class="form-horizontal" enctype="multipart/form-data"
method="POST" action="{{route('user.update', Auth::user()->id)}}">
<input type="submit" class="btn btn-primary" name="save" value="Update"/>
And I have route set up as:
Route::resource('user', 'UserController');
You need to add PUT method into your form.
<input name="_method" value="PUT" type="hidden">
<input type="hidden" value="{{ csrf_token() }}" name="_token">

Unable to delete in laravel

I am using laravel 5.2 and I am unable to delete article in laravel. Below is my view link:
<form method="DELETE" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Below is my controller code:
public function destroy($id)
{
Article::destroy($id);
Session::flash('msg','Article deleted successfully');
return redirect()->back();
}
Below are route listing:
HTML forms don't actually support any methods other than GET and POST. To get around this Laravel spoofs the method and then picks this up in the request.
From the docs:
HTML forms do not support PUT, PATCH or DELETE actions. So, when
defining PUT, PATCH or DELETE routes that are called from an HTML
form, you will need to add a hidden _method field to the form. The
value sent with the _method field will be used as the HTTP request
method
As such, you just need to alter your form like so:
<form method="POST" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
You can also generate the _method with {{ method_field('DELETE') }} using Blade.
In your view file what you need to do is...
<form method="POST" action="/article/{{ $article->id }}">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>

Laravel 5.1 form hidden _method does not work

I'm having issues with getting a laravel app to update or delete a resource.
Here is my view.
#extends('admin.master')
#section('content')
<h1>Create an Article</h1>
<form action="/articles/{{ $article->id }}">
<input type="hidden" name="_method" value="PUT">
{!! csrf_field() !!}
#include('admin.partials.forms.article')
<div class="row">
<button type="submit" class="btn btn-success btn-lg">Update Article</button>
</div>
</form>
#endsection
Here is my controller
public function update($id, Request $request)
{
return "Update Article Code Here!";
}
All I get when I submit the form is a blank page with the url
app.dev/articles/1?_method=PUT&_token=LL6Z5zHNUG1dLjjH2TDpXXCWbGnfiCKTY4cuoVbm&title=Our+Upcoming+Event+Now+Updated&description=a+brief+event+description&body=Updated+Body&category=Events
The issues is that while you have to have the hidden method to allow laravel to see what you're doing, you also have to have the method="POST".
<form action="/articles/{{ $article->id }}" method="POST">
<input type="hidden" name="_method" value="PUT">

Categories