Missing required parameters for route issue in laravel - php

In my laravel based application I have following link in my admin.blade.php
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="{{ route('cms.home.create') }}" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>{{ __('Home Page') }}</p>
</a>
</li>
</ul>
In my project I have another blade called, create.blade.php which is in the following path
views/cms/home/create.blade.php
I have a controller called, CmsHomeController.php for that blade
In CmsHomeController I have a method called create
public function create()
{
return view('cms.home.create');
}
Once the user clicks on the above mentioned link in the admin.blade.php, user should go to the create.blade.php blade.
And in my web.php I have registered my route as follows,
Route::resource('cms.home','CmsHomeController');
But now the issue is,
When I click on that link in admin blade, I'm getting an error saying
Facade\Ignition\Exceptions\ViewException
Missing required parameters for [Route: cms.home.create] [URI: cms/{cm}/home/create]. (View: C:\xampp\htdocs\mylaravelproject\resources\views\layouts\admin.blade.php)
In create.blade.php , I'm having just a simple form
Where am I doing wrong and what would be the correct fix?
UPDATE:
I tried running
php artisan route:list
This is what I got
I don't have such a param called, 'cm'..

As you are using dot (.) operator in your resource route it will generating nested routes.
You need to change your route name with single name like cms-home or cms_home
Then you can simple use it as:
Route::resource('cms_home','CmsHomeController');
In your blade you can call it:
<a href="{{ route('cms-home.create') }}" class="nav-link">
Please have look at this referance

Your route need a parameter (cm)
You should have in your template href="{{ route('cms.home.create', ['cm' => $id_cm]) }}"

If you need the route be cms.home, try this:
Route::resource('cms.home', 'CmsHomeController', [
'parameters' => ['cms' => 'cms']
]);

Just Pass the value in your route cms.home.create like this.
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="{{ route('cms.home.create', ['cm' => $value]) }}" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>{{ __('Home Page') }}</p>
</a>
</li>
</ul>

Related

Laravel The GET method is not supported for this route when redirecting back()

I have Laravel project and there is a post route that I pass a parameter in the URL and a form in the Request
Route::post('/gc/verifierQR-{qr_url}', [UserController::class, 'inquiryResult'])->name('inquiry.result');
this route returns to a view file and in this view there is change language button
<div class="nav-item">
<div class="dropdown">
<button class="bg-transparent border-0" type="button" id="dropdownMenuButton1"
data-bs-toggle="dropdown" aria-expanded="false">
<a class="nav-link text-white"><i class="fas fa-globe me-2"></i> العربية <i
class="fas fa-chevron-down"></i></a>
</button>
<ul class="dropdown-menu border-0 shadow" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item fw-bold" href="{{ route('lang', $arabic_locale) }}"><img
src="{{ asset('images/egypt-flag.png') }}" class="egypt-flag">
العربية</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="{{ route('lang', $english_locale) }}"><img
src="{{ asset('images/usa-flag.png') }}" class="egypt-flag">
English</a></li>
</ul>
</div>
</div>
the route of language changing
Route::get('/language/{locale}', function ($locale) {
app()->setLocale($locale);
session()->put('locale', $locale);
return redirect()->back();
})->name('lang');
but in this page when I change the language and redirect back() I found this error
The GET method is not supported for this route. Supported methods: POST.
When we say redirect, we usually mean the browser, and you can not change the method which the browser uses to open pages (which is GET).
Either redirect to another separate route.
Or allow both GET and POST methods (for your already existing route).
Instead of normal POST route:
Route::post('my-url', function () {
return view('my-view');
})->name('my-route');
Do something like:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::match(['post', 'get'], 'my-url', function (Request $request) {
return view('my-view');
})->name('my-route');
The answer is simple. The route or url where you returned back definately a post route. you can use any instead of get and post.

make $user is undefined Make the variable optional in the blade template. Replace {{ $user }} with {{ $user ?? '' }}

I have one-page sidebar.blade.php and I have to import this page in another page as a sidebar
I have variable here that is coming from the controller when I import sidebar to other page using #extends it shows me error undefined variable with solution
" make $user is undefined Make the variable optional in the blade template. Replace {{ $user }} with {{ $user ?? '' }}"
how I can solve this
sidebar code here
#if($user->role =="handlers" )
<li>
<i class="fa fa-user fa-fw"></i> kontaktuppgifter{{-- <span class="fa arrow"></span> --}}
</li>
#endif
<li>
Since Your probably not passing the variable $user in to each of your views, but you are extending the sidebar view, try using Laravel auth() helper instead to get the user model. You can do something like this:
#if(auth()->user()->role == "handlers")
<li>
<a href="{{ url('/kontaktuppgifter') }}">
<i class="fa fa-user fa-fw"></i>
kontaktuppgifter
</a>
</li>
#endif
You can also share a $user variable with all your views, this way you won't have to query the database each time you want to retrieve the user data.
You can do this in your BaseController class __construct() function:
//App\Http\Controller.php
public function __construct()
{
$user = auth()->user();
view()->share('user', $user);
}
Then you can use a user variable in your view, just like you are doing right now:
#if($user->role == "handlers")
<li>
<a href="{{ url('/kontaktuppgifter') }}">
<i class="fa fa-user fa-fw"></i>
kontaktuppgifter
</a>
</li>
#endif
If $user is current user, then You should use Auth for that:
#if(auth()->user()->role =="handlers" )
<li>
<i class="fa fa-user fa-fw"></i> kontaktuppgifter{{-- <span class="fa arrow"></span> --}}
</li>
#endif

On every click link changes in blade view

This is the blade view to create the url links
#section ('category')
<ul class="main-categories">
#foreach($category as $category)
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#endforeach
</ul>
#endsection
when i click for the first time it's working fine and the url I get is
http://localhost/shopnew/public/glassfilm/Frosted
when I click for the secon time the url I get is
http://localhost/shopnew/public/glassfilm/glassfilm/Frosted
what I am doing wrong?
Change your route as below :
#section ('category')
<li class="main-nav-list">
<a href="{{ route('glassfilm-category',['name' => $category->category]) }}"> {{ $category->category }}
<span class="number">{{ $category->count}}</span>
</a>
</li>
#endsection
asset() method is used to include CSS/JavaScript/images files.
url() method used to generate a URL to a link.
Laravel's route() method is very helpful. So you can try:
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#section ('category')
<ul class="main-categories">
#foreach($category as $category)
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#endforeach
</ul>
#endsection

Cannot use Auth::user() in blade template in Laravel 5.4

I want to make my index page with both guest and auth functions.
When guests visit, they could try logging in; And after login, it will show the user's info such as username.
Below is what I did.
Routes are defined in routes/web.php:
Route::get('/', 'CommonController#index');
Auth::routes();
In blade template of index, I used Auth::user() to validate authentication but failed.
#if(Auth::user())
<div id="user-panel">
<a href="" class="user-panel-button">
New Article
</a>
<div class="btn-group user-panel-button">
<a type="button" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>Logout</li>
</ul>
</div>
</div>
#else
<a href="{{ url('login') }}" class="button">
Login
</a>
#endif
Every time I logged in successfully, it will still show login button instead of user name button, after I REFRESH the index page.
I have checked session information, once I refreshed the page, the login_web_xxxxxxxxxxx session will be disappeared.
Furthermore, When I use Laravel default path for auth /home, it worked. Username could always be presented whatever how many times you refresh the page.
How should I fix this issue? Many thanks.
For Laravel 5.4 you can use:
#if(Auth::check())
// The user is authenticated...
#else
// The user is not authenticated...
#endif
For Laravel 5.5 and above:
#auth
// The user is authenticated...
#endauth
#guest
// The user is not authenticated...
#endguest
Instead of using
<ul class="dropdown-menu">
<li>Logout</li>
</ul>
For log out, it is better to be codes as below, which is provided by official resources/view/layouts/app.php
<ul class="dropdown-menu">
<li>
<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>
</li>
</ul>
Update routes/web.php file as
Route::group(['middleware' => 'web'], function () {
Route::get('/', 'CommonController#index');
Auth::routes();
}
Now to check auth in your blade template, you can use
#if(\Auth::check())
....
....
#else
....
....
#endif
also you can use the full class path of Auth in blade file like this
#if(\Illuminate\Support\Facades\Auth::check())
// code here
#endif

Automatic translation does not work in Laravel 5.2

After changing the locale name and then print {{ Config::get('languages')[App::getLocale()] }}, it's giving me the locale name i.e. Italiano or Français. But it does not translate the website text into changed locale!
I am using following article for the localization Laravel 5.2 localization also tried Laravel Localizationbut same also there.
From my understanding, it should translate the language automatically!
Need some expert help from the community.
Sample code from my view file:
#extends('layouts.app')
#section('content')
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{ Config::get('languages')[App::getLocale()] }}
</a>
<ul class="dropdown-menu">
#foreach (Config::get('languages') as $lang => $language)
#if ($lang != App::getLocale())
<li>
{{$language}}
</li>
#endif
#endforeach
</ul>
</li>
<h2>Welcome to my website</h2>
<p>Please find your desired content by using website search function.</p>
#endsection
Are you expecting the text elements to translate? You need to use the trans() function and put your copy text in translation files.
EG
<h2>{{trans('welcome_message')}} </h2>
Then stick the copy in your language file
You are getting languages from nowhere, so if you want it to work, copy/paste following array in your app.php
'locales' => [
'en' => 'English',
'ka' => 'Georgian',
'ru' => 'Russian',
],
And if you call Config::get('app.locales') you will get this locales.
Here is how your code must look like:
#extends('layouts.app')
#section('content')
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{ Config::get('app.locales')[App::getLocale()] }}
</a>
<ul class="dropdown-menu">
#foreach (Config::get('app.locales') as $lang => $language)
#if ($lang != App::getLocale())
<li>
{{$language}}
</li>
#endif
#endforeach
</ul>
</li>
<h2>Welcome to my website</h2>
<p>Please find your desired content by using website search function.</p>
#endsection

Categories