Laravel route not defined error even though it is - laravel

I have the following blade code:
<a href="{{ route('settings') }}">
<span class="title">Settings</span>
</a>
I have the following defined at the top of my routes.php file:
Route::post('settings/update', 'SettingsController#update');
Route::resource('settings', 'SettingsController');
When I try to go to any page with route('settings') I get a Route [settings] not defined error.
If I do php artisan routes I can see that the settings routes are all there as expected.

With route('settings') you refer to a route named settings but you don't have such a route. RESTful routes will automatically receive a route name though.
For the index method it is resource.index for the show method resource.show and so on.
Change your code to this:
<a href="{{ route('settings.index') }}">
<span class="title">Settings</span>
</a>

Change {{ route('settings') }} to {{ URL::to('settings') }}

Related

How to pass multiple arguments with url (routing) in laravel

I am trying to pass argument on route but unfortunatly i am facing error please help me thanks.
Missing required parameters for [Route: products.detail] [URI:
products/{slug}/{id}/details]. (View:
/home/zubair/htdocs/wallmasterfurnishing.com/web/resources/views/wallpapers-product.blade.php)
let detailurl = '{{route("products.detail",[":slug",":id"])}}';
productdetail = detailurl.replace(':slug', element.slug);
productdetail = detailurl.replace(':id', element.id);
<a href="${productdetail}">
<h4 class="prodcardtitle">${element.code} - ${element.name}</h4>
</a>
It seams like you are combining Blade template with Javascript
let detailurl = '{{route("products.detail",[":slug",":id"])}}';
let keywords is part of JavaScript
and {{ route("products.details", [$element->slug, $element->id] }} is only available inside of blade template
As you are inside of javascript and not in blade template you should create the url using template literals and replace all placeholder with the the related value like this
let detailurl = `/products/${element.slug}/${element.id}/details`;
`<a href="${productdetail}">
<h4 class="prodcardtitle">${element.code} - ${element.name}</h4>
</a>`
You can pass multiple parameter to route as an array
<a href="{{ route('products.detail', ['slug' => $slugVariable, 'id' => $idVariable]) }}">
<h4 class="prodcardtitle">${element.code} - ${element.name}</h4>
</a>
for more info visit
Thanks
try with route name :
Route::get('products/{slug}/{id}/details','ProductController#productDetail')->name('productDetail');
<a href="{{ route('productDetail', ['laptop-slug','2']) }}">
<h4 class="prodcardtitle">${element.code} - ${element.name}</h4>
</a>

product detail page is not showing

hi m trying to get product details by clicking on product at product listing page, when I click on product link then at next page URL is fine http://127.0.0.1:8000/product/2 but product is not showing
controller:
public function productdetail(Request $request, Product $product)
{
return view('product.detail', compact('product'));
}
route:
Route::get('/product/{product}','Admin\ProductController#productdetail')->name('product.productdetail');
detail.blade.php
<form method="POST" action="{{ route('product.productdetail', $product->id) }}" enctype="multipart/form-data">
#csrf
{{ $product->product_name }}
</form>
at listening page m using this link for detail page:
<a href="{{ route('product.productdetail', $product->id) }}" class="block2-name dis-block s-text3 p-b-5">
{{ $product->product_name }}
</a>
Ok, I found where problem is when you provide your controller and routes.
You got route resource:
Route::resource('product','ProductController');
And your route for details:
Route::get('/product/{product}','Admin\ProductController#productdetail')->name('product.productdetail');
Resource will provide same route for show, so you need to change your route with additional parameter like:
Route::get('/product/{product}/details','Admin\ProductController#productdetail')->name('product.productdetail');
Now you can point to that route and I think now will work.

laravel : Pass information with key in url

Hello i want to make url like example.com/overview-schedule?=user_key=123456 in laravel. I do not know how to do it.
Sharing my code :
In blade.php define a link
<a href="{{ route("overview-schedule") }}">
on my web.php
Route::any('overview-schedule', 'Front\PlanController#scheduleOverview')->name('overview-schedule');
Got an error
InvalidArgumentException
Route [overview-schedule?user_key=123456] not defined.
How can i do it ?
use url() helper method
$user_key = 123456;
<a href="{{ url('/overview-schedule?user_key='.$user_key) }}">
You can pass an array of parameters to route function :
<a href="{{ route("overview-schedule" , ['user_key' => $user_key]) }}">
You should try this:
<a href="{{ url('/overview-schedule?user_key='.$user_key) }}">

Logging out via a link in Laravel

I have a "Logout" link in my top navigation bar. I'm wondering how I can make it so that while I'm logged in, it'll log me out when I click on it and return me to the homepage.
To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?
When you run php artisan make:auth, the default app.php in Laravel 5.5 does it like this:
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante maybe a better choice. Refer to the comment section of this answer.
1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:
logout
then add this to your web.php file
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController#logout');
Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run
php artisan make:auth
2) add this to your navigation bar:
logout
then add this in your web.php file
Route::get('/logout', 'YourController#logout');
then in the YourController.php file, add this
public function logout () {
//logout user
auth()->logout();
// redirect to homepage
return redirect('/');
}
Done.
Read:
https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
Use the logout() method:
auth()->logout();
Or:
Auth::logout();
To log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session.
if you want to use jQuery instead of JavaScript:
<a href="javascript:void" onclick="$('#logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
As the accepted answer mentions that logging out via GET has side effects you should use the default POST route already created by Laravel auth.
Simply create a little form and submit it via link or button HTML tag:
<form action="{{ route('logout') }}" method="POST">
#csrf
<button type="submit">
{{ __('Logout') }}
</button>
</form>
If you use guard you can logout using this line of code :
Auth::guard('you-guard')->logout();
in laravel 8.x
#csrf
<x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Logout') }}
</x-jet-dropdown-link>
</form>

How to define a link in laravel?

Hello I am new bee in laravel. I have an <a> tag. I want to give link in controller. I wrote
<a href="{{ URL::to('/controller'); }}">
But it prints same in addressbar:
http://localhost:8000/{{ URL::to('/CategoryController'); }}
My routes.php file is OK.
Should i need to change in any other file ?

Resources