Laravel 404 not found when passing parameter with route() - laravel

Hello I have instructors table, and every instructor have name column and I have a page to show events and every event has instructor name and I need to go to the instructor profile page when clicking on the instructor name in the events page so I made this
<a href="{{ route('instructor.profile', ['name' => $event->instructor]) }}" class="a-reset">
<h5 class="color-primary fw-bold">{{ $event->instructor }}</h5>
</a>
web.php
Route::get('/instructor/{name}', 'App\Http\Controllers\InstructorController#profile')->name('instructor.profile');
InstructorController
public function profile($name) {
$instructor = Instructor::where('name', '=', $name)->first();
return view('instructor-profile', compact('instructor'));
}
But it gives me 404 Not Found error page

Make sure it returns value $event->instructor
Make sure the address is correct instructor-profile
by this command rtisan route:list open route list, make sure it is there /instructor/{name} route.
if everything is correct run this command
php artisan route:clear
php artisan config:cache

Related

GET (405) method not allowed [duplicate]

Im trying to do a POST request with jQuery but im getting a error 405 (Method Not Allowed), Im working with Laravel 5
THis is my code:
jQuery
<script type="text/javascript">
$(document).ready(function () {
$('.delete').click(function (e){
e.preventDefault();
var row = $(this).parents('tr');
var id = row.data('id');
var form = $('#formDelete');
var url = form.attr('action').replace(':USER_ID', id);
var data = form.serialize();
$.post(url, data, function (result){
alert(result);
});
});
});
</script>
HTML
{!! Form::open(['route' => ['companiesDelete', ':USER_ID'], 'method' =>'DELETE', 'id' => 'formDelete']) !!}
{!!Form::close() !!}
Controller
public function delete($id, \Request $request){
return $id;
}
The Jquery error is http://localhost/laravel5.1/public/empresas/eliminar/5 405 (Method Not Allowed).
The url value is
http://localhost/laravel5.1/public/empresas/eliminar/5
and the data value is
_method=DELETE&_token=pCETpf1jDT1rY615o62W0UK7hs3UnTNm1t0vmIRZ.
If i change to $.get request it works fine, but i want to do a post request.
Anyone could help me?
Thanks.
EDIT!!
Route
Route::post('empresas/eliminar/{id}', ['as' => 'companiesDelete', 'uses' => 'CompaniesController#delete']);
The methodNotAllowed exception indicates that a route doesn't exist for the HTTP method you are requesting.
Your form is set up to make a DELETE request, so your route needs to use Route::delete() to receive this.
Route::delete('empresas/eliminar/{id}', [
'as' => 'companiesDelete',
'uses' => 'CompaniesController#delete'
]);
Your routes.php file needs to be setup correctly.
What I am assuming your current setup is like:
Route::post('/empresas/eliminar/{id}','CompanyController#companiesDelete');
or something. Define a route for the delete method instead.
Route::delete('/empresas/eliminar/{id}','CompanyController#companiesDelete');
Now if you are using a Route resource, the default route name to be used for the 'DELETE' method is .destroy. Define your delete logic in that function instead.
In my case the route in my router was:
Route::post('/new-order', 'Api\OrderController#initiateOrder')->name('newOrder');
and from the client app I was posting the request to:
https://my-domain/api/new-order/
So, because of the trailing slash I got a 405. Hope it helps someone
If you didn't have such an error during development and it props up only in production try
php artisan route:list to see if the route exists.
If it doesn't try
php artisan route:clear to clear your cache.
That worked for me.
This might help someone so I'll put my inputs here as well.
I've encountered the same (or similar) problem. Apparently, the problem was the POST request was blocked by Modsec by the following rules: 350147, 340147, 340148, 350148
After blocking the request, I was redirected to the same endpoint but as a GET request of course and thus the 405.
I whitelisted those rules and voila, the 405 error was gone.
Hope this helps someone.
If you're using the resource routes, then in the HTML body of the form, you can use method_field helper like this:
<form>
{{ csrf_field() }}
{{ method_field('PUT') }}
<!-- ... -->
</form>
It will create hidden form input with method type, that is correctly interpereted by Laravel 5.5+.
Since Laravel 5.6 you can use following Blade directives in the templates:
<form>
#method('put')
#csrf
<!-- ... -->
</form>
Hope this might help someone in the future.
When use method delete in form then must have to set route delete
Route::delete("empresas/eliminar/{id}", "CompaniesController#delete");
I solved that issue by running php artisan route:cache which cleared the cache and it's start working.
For Laravel 7 +, just in case you run into this, you should check if the route exists using
php artisan route:list
if it exists then you need to cache your routes
php artisan route:cache

Laravel route returning error 404 when attempt is made to pass value to controller function

I have a button in my blade like this
#can('customer_show')
<a class = "btn btn-primary" href = "{{ route('admin.loan-applications.showCustView', $loanApplication->user_id) }}">
View Applicant
</a>
#endcan
And this is the route:
Route::get('loan-applications/{loan_application}/showCustView', 'loanApplicationsController#showCust')->name('loan-applications.showCustView');
And in my controller, i did:
public function showCust(LoanApplication $loanApplication)
{
$customerInformation = customerInfoModel::where('Cust_id', $loanApplication->user_id));
return view('admin.loanApplictions.showCustView', compact(['customerInformation', 'loanApplication']));
}
What i am trying to do is fetch the row from the database attached to customerInfoModel where the the Cust_id field equals the loanApplication->user_id of the loan being viewed currently in the blade above. When the button "view Applicant" is hit, i get an error 404 page. Why is that so?
Check it out the route list using this command
php artisan route:list
//this command will show all the routes in your application
If your route not listed on that route list checkout for routes with same Url on your route manually on routes file.
if you found change the url & use this command to clear cache
php artisan optimize:clear
i have found the comment of you on last answer.
check out for route model binding . i think you have to add
a
public function showCust( $loanApplicationCustId)
{
$customerInformation = customerInfoModel::where('Cust_id', $loanApplicationCustId))->first();
return view('admin.loanApplictions.showCustView', compact(['customerInformation', 'loanApplication']));
}
It should be like this .. i hope it works for you......
else share your project git repo link
I think it should be like following:
#can('customer_show')
<a class = "btn btn-primary" href = "{{ route('loan-applications.showCustView', $loanApplication->user_id) }}">
View Applicant
</a>
#endcan
try that

Route not found even though It's defined

This is the error that I get rolled out
(View: Route [user.profile] not defined. (View: D:\Programi\XAMPP\htdocs\crushapp\resources\views\layouts\base.blade.php)
But my route is clearly defined here:
use App\Http\Livewire\User\ProfileComponent;
Route::middleware(['auth:sanctum','verified'])->group(function()
{
Route::get('/user/profile',ProfileComponent::class)->name('user.profile');
});
The error comes from the layout, namely:
<span><i class="fa fa-user-circle-o" aria-hidden="true"></i> {{ Auth::user()->name }}  ‎‎‏‏‎ ‎‎‏‏</span>
Any ideas what could it be?
UPDATE: Any new route I try defining under user won't work...
first you are missing a function name in your route
also run php artisan route:clear
Route::get('/user/profile',ProfileComponent::class, 'index')->name('user.profile')

problem in delete from pivot table in laravel

i have a problem in delete an order:
here the form where press on cancel to delete the order:
<form action="{{route('user.orders.delete', $order->id)}}" method="POST"
onsubmit="return confirm('Are you sure?');">
#method('DELETE')
#csrf
<button
type="submit" class="mt-8 mb-4 py-2 px-14 rounded-full
bg-red-900 text-white tracking-widest hover:bg-blue-800
transition duration-200">
Cancel
</button>
</form>
this is my router:
Route::delete('user/orders/delete/{order}', [OrderController::class, 'delete'])->name('user.orders.delete');
OrderController : delete function:
public function delete(Order $order)
{
try {
unlink(public_path().'/storage/orders/files/'.$order->file);
$order->delete();
}catch(\Exception $e) {
return redirect()->back()-with('status', 'you cannot delete this item');
}
return redirect()->back()->with('status', 'product deleted succefully');
}
so when click on the button (cancel) just go to the right url : when 21 is the order id
http://127.0.0.1:8000/user/orders/delete/21
and it is just not found page!!! and nothing deleted!!!
i think your app routes is cached
try to run the fallowing command (in cmd or terminal) to see all the routes, this will show registered routes and errors if exist:
php artisan route:list
if the route doesnt exist in route list, the route list may be cached. try the fallowing command to clear route cache:
php artisan route:clear
and then run app and test the route.
If you do not have another problem in coding or naming variables, the program will be fixed properly
*if all o the above is ok, it seems your apache config is not configure to delete request(for route delete method ) and you must change .htaccess requests LIMIT like the fallowing sample to Grant delete request :
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
As you can see the DELETE method(request) has been added to .htaccess

NotFoundHttpException in RouteCollection.php (line 179) dont fun

I have a problem in laravel, I have developed a system that worked correctly the routes, but now it does not work any route all give me the same result error.
Esta mi archivo app.blade.php
<li>
<a href={{url('home')}} class="dropdown-toggle">
<i class="fa fa-dashboard"></i>
<span class="hidden-xs">Panel</span>
</a>
</li>
This is my web.php file
Auth::routes();
Route::get('/', function () {
return view('home');
});
Route::get('/home', 'PruebaController#index')->name('home');
this is my Controller file
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PruebaController extends Controller
{
public function index()
{
return view('home');
}
}
php artisan route:list
I have reviewed everything but I do not find the problem, everything seems to be correct, what I did was to clone the project have something to do.
Dou yo have view->home.blade.php?
Since you mentioned that you have cloned the project, I think its got to do with cache.
Try
php artisan cache:clear
php artisan config:cache
I followed the error to the file RouteColletion.php in the function matchAgainstRoutes when I print the $ request, the last variables
Request {#38 ▼
...
pathInfo: "/ gesis / public / home"
requestUri: "/ gesis / public / home"
baseUrl: ""
basePath: ""
format: "html"
}
I leave empty I think that's why but I do not know why I leave these variables empty.
I corrected it thanks to all, the problem was the simple truth but I could not see the directive APP_NAME = geSis of the file .env changed the uppercase S by the small one so it did not encintrace the route.
I thanked them for their time and I received a lot of comments.

Resources