Laravel Not found route after using Language switcher - php

Hello everyone I have an issue I implemented language switcher however there seems to be an issue for pages that got id parameter as the pictures below shows it's no longer recognizable
Before action Image
After Action Image : Error
Routes :
Route::redirect('/', '/fr');
Route::group(['prefix'=>'{language}'],function(){
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/projets', [App\Http\Controllers\ProjectController::class, 'index'])->name('projects.index');
Route::get('/projets/create',[App\Http\Controllers\ProjectController::class, 'create'])->name('projects.create');
Route::post('/projets',[App\Http\Controllers\ProjectController::class, 'store'])->name('projects.store');
Route::get('/projets/{id}', [App\Http\Controllers\ProjectController::class, 'edit'])->name('projects.edit');
Route::put('/projets/{id}', [App\Http\Controllers\ProjectController::class, 'update'])->name('projects.update');
Route::delete('/projets/{id}', [App\Http\Controllers\ProjectController::class, 'destroy'])->name('projects.destroy');
});
Code:
<td><a class="btn btn-success" data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="{{ route('projects.edit',['language' =>app()->getLocale(),'id' => $project->id]) }}">Update</a>
</td>
<td>
<form action="{{ route('projects.destroy', ['language' => app()->getLocale(),'id' =>$project->id])}}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete" type="submit" onclick="return confirm('Etes-vous sûr de la suppression?');" >Delete</button>
</form>

Related

Laravel CRUD - Delete - Not route found

Error:
Route [events.destroy] not defined. (View: C:\xampp\htdocs\Event-Manager-Project\resources\views\events\admin.blade.php)
In my view, admin i have this about the delete:
<form action="{{ route('events.destroy', $event->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="far fa-trash-alt" ></button>
</form>
In controller this:
public function destroy($id)
{
$event = Event::find($id);
$event->delete();
return redirect('/admin');
}
And using a route resource:
Route::resource('admin', 'App\Http\Controllers\EventController');
change the code to this
<form action="{{ route('events.destroy', $event->id) }}" method="post">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="Delete" name="Delete" id="btnExc" class="btn btn-sm btn-danger glyphicon glyphicon-trash" accesskey="x"/>
</form>

$request->has() doesn't work with Button type and using jQuery in Laravel controller

I have question on Laravel with jQuery.
Right now, I have form with three (3) different buttons (update,delete,email). Each button have different task in controller.
Normally I use button type="submit" to submit data from blade into controller.
Blade
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="submit" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="submit" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="submit" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
And in controller, I just use '$request->has()' to differentiate three request.
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
But my current goal is to show loading icon before data successfully submitted.
The reason why I want to show loading page is because system need to send an email to multiple user and it might take some time to load before it finished the task. To prevent user to click the same button multiple times.
I use jQuery click function and I change button type from 'submit' to 'button'.
But the problem is when the form submitted to controller. It doesn't recognize '$request->has()' in my controller.
Here's my html form and jQuery and my controller
blade.php
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="button" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="button" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="button" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
jQuery
$(document).ready(function(){
$('#update').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#delete').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#email').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
});
Controller.php
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
How to solve this problem ? Thanks in advance.

What could be the issue in my route that gets me MethodNotAllowedHttpException for my PUT, PATCH, DELETE and others routes

Can you please help me with my issue. I am trying my best to fix the issue, but so far I have failed.
I even upgraded to Laravel 7.x with my application and still It kept on getting the same issue over and over again. Could my issue be in the user interface (UI)?
below are the errors that i'm getting:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST.
This is my route
Route::get('/', function () {
//return redirect(route('login'));
return redirect()->route('login');
});
Auth::routes();
Route::middleware(['auth']) -> group(function() {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/device', 'DeviceController');
Route::get('/laptops', 'LaptopsController#index')->name('laptops');
});
This is my form edit
<form action="/device" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
This is my delete form
<form action="/device" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<input type="hidden" name="_method" value="DELETE" >
<P>Are You Sure!.. You want to delete this Device?</P>
</div>
<button class="btn btn-primary " type="submit" >YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal" >CANCEL</button>
</form>

The DELETE & PUT method is not supported for this route. Supported methods: GET, HEAD,POST

I need your help guys on this, my app can't delete neither can it Update. it keeps on popping these errors on my edit and Delete Buttons;
(1/1) MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD.
(1/1) MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.
this is my route
Route::group(['middleware' => ['role:Admin']], function () {
Route::resource('/device', 'DeviceController');
});
this is my edit blade
<form action="/device" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
This is my delete blade
<form action="/device" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<input type="hidden" name="_method" value="DELETE" >
<P>Are You Sure!.. You want to delete this Device?</P>
</div>
<button class="btn btn-primary " type="submit" >YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal" >CANCEL</button>
</form>
This is my controller for delete
public function destroy($id)
{
$devices = Device::find($id);
$devices -> delete();
return Redirect::back() -> with('success','Data Deleted Successfully');
}
This is my script that deletes
<script>
//Start Delete Record
table.on('click', '.delete', function () {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#deleteForm').attr('action', '/laptops/'+data[0]);
$('#deleteModal').modal('show');
});
//End Delete Record
});
</script>
You are not passing the correct actions
your update form should look like this instead
<form action="" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
your delete form should look like this instead.
<form action="" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<p>Are You Sure!.. You want to delete this Device?</p>
</div>
<button class="btn btn-primary " type="submit">YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal">
CANCEL
</button>
</form>
your script should look like this (change 'laptops/' to 'device/')
table.on('click', '.delete', function () {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#deleteForm').attr('action', '/device/'+data[0]);
$('#deleteModal').modal('show');
});
Also as an opinion you could pluralize your resource route instead of the singular approach. i.e /devices instead of /device
so in your web.php routes file you have:
Route::group(['middleware' => ['role:Admin']], function () {
Route::resource('/devices', 'DeviceController');
});
That way you have a more friendly route list like:
GET - /devices
GET - /devices/create
POST - /devices/update
GET - /devices/{device}
GET - /devices/{device}/edit
PUT/PATCH - /devices/{device}
DELETE - /devices/{device}
All these can be viewed in your cli using php artisan route:list

Laravel - Approve and decline buttons on one form for same controller with form action

I have two buttons on my form, Approve and Decline
{{ Form::open(['route' => ['holidays.update', $holidayRequest->id]]) }}
<button type="submit" class="btn btn-success">
<i class="fa fa-check"></i>Approve
</button>
<button type="submit" class="btn btn-danger">
<i class="fa fa-times"></i>Decline
</button>
{{ Form::close() }}
There is an object called $holidayRequest which I have passed to this form from the controller, and my intention is to send a value of the $holidayRequest->status back to the controller. That is, one of the following should occur:
User clicks on Approve: $holidayRequest->status == 'approved' is sent to controller.
User clicks on Decline: $holidayRequest->status == 'declined' is sent to controller.
Should I use the attribute formactionto achieve this goal or something else? How do I go about this with as little or no JS as possible?
Try something like this using jquery():
Html:
<button type="button" id="approved" class="btn btn-success response">
<i class="fa fa-check"></i>Approve
</button>
<button type="button" id="declined" class="btn btn-danger response">
<i class="fa fa-times"></i>Decline
</button>
Jquery:
$('.response').click(function(){
var resposne = $(this).attr('id');
// It gives you: approved or declined
// make an ajax call with this response and update the data in database
});
Note: Do not forget to include jquery library in your page.
If you don't want javascript then try submitting them individually.
{{ Form::open(['route' => ['holidays.update', $holidayRequest->id]]) }}
<input type="hidden" status="approved"/>
<button type="submit" class="btn btn-success">
<i class="fa fa-check"></i>Approve
</button>
{{ Form::close() }}
{{ Form::open(['route' => ['holidays.update', $holidayRequest->id]]) }}
<input type="hidden" status="declined"/>
<button type="submit" class="btn btn-danger">
<i class="fa fa-times"></i>Decline
</button>
{{ Form::close() }}

Categories