post route in Laravel - php

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']);

Related

The GET method is not supported for this route. Supported methods: DELETE

I got this error when I try to remove subcategories by however I did use the delete method.
blade.php:-
<form action="{{ url('sub_category/delete',$item->id) }}" method="DELETE">
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
my web
Route::DELETE('/sub_category/delete/{id}','SubcategoryController#destroy');
controller:-
public function destroy($id)
{
$subcategory= Subcategory::where('id',$id)->delete();
return redirect()->route('cars.index');
// ->with('success','Car deleted successfully');
}
Your form should be in the following way
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
<input name="_method" type="hidden" value="DELETE">
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
DELETE,PUT,PATCH,HEAD methods should be defined as follows
#method('DELETE') #method('PUT') ...
Laravel routing
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
try like this one:
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
{{method_field('DELETE')}}
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
your route:
Route::delete('/sub_category/delete/{id}','SubcategoryController#destroy');

Laravel - Edit and Update Page

I am using Laravel and I am trying to create an edit page and call my update method on submit, the problem is I am getting a 404 when updating. This is my blade file for editing like so:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Here are my routes:
Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController#edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController#update');
And Here are the methods being called:
public function edit($id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
public function update(Request $request, $id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
Why am I getting a 404 error and how do I fix it?
Thanks,
In laravel 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:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
You may use the #method Blade directive to generate the _method input:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
There are so many issues in your code lets resolve one by one:
action="/admin/professions-update/{{ $data->pkprofession }}">
change it to:
action="{{ url('/admin/professions-update/' . $data->pkprofession) }}">
and then HTML forms do not support PUT, PATCH or DELETE actions, so chage it to:
<form action="{{ url('/admin/professions-update/' . $data->pkprofession) }}" method="POST">
#method('PUT')
#csrf // this is required when you are using the method other then 'get'
other elements
</form>
You're missing the csrf token and the method input. Try this:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="POST" action="/admin/professions-update/{{ $data->pkprofession }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Also, in your update method you are forgeting to update the object, add this to your code:
$data->update($request->all());
For more info: DOCS

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.

Delete a record gave me NotFoundException in laravel 5

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/

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">

Categories