Adding prefix as a variable in route::group in laravel - php

I got this error message when i am trying to add prefix as a variable in route group
error message :
UrlGenerationException in UrlGenerationException.php line 17:
Missing required parameters for [Route: client.login] [URI: login].
web.php :
Route::group(['prefix' => '{account}'], function()
{
Route::GET('login', ['as' => 'client.login', 'uses' => 'Client\Auth\LoginController#showLoginForm']);
}
controller :
public function showLoginForm()
{
return view('client.auth.login', $this->data);
}

Your controller function must accept as arguments the URL params defined in the routes.
In this case it should be
public function showLoginForm($account)
{
return view('client.auth.login', $this->data);
}
But you probably want to do something with account

Related

Laravel 8 : Route with parameter in Routegroup don't work

i've trouble with laravel localization and route in grouproute.
I try to access this url :
http://localhost/logiluxe/public/en/games_giveaways/1
My routes :
Route::group(['middleware' => ['setlocale'],'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function() {
Auth::routes();
Route::get('/games_giveaways/{id}', [GameGiveawayController::class, 'show'])->name('games_giveaways.show');
});
My middleware :
public function handle(Request $request, Closure $next) {
if(session()->has('locale')){
app()->setLocale(session()->get('locale'));
//Carbon::setLocale(session()->get('locale'));
} elseif ($request->segment(1)) {
app()->setLocale($request->segment(1));
} else {
app()->setLocale('en');
}
return $next($request);
}
The error :
Missing required parameter for [Route: games_giveaways.show] [URI: {locale}/games_giveaways/{id}]
[Missing parameter: id]. (View: C:\wamp64\www\logiluxe\resources\views\layouts\app.blade.php)
It's look like i can't have a route with parameter in a routegroup.
Your error is in the balde, not the route declaration itself.
Somewhere in your blade you are calling the route by alias without the ID parameter.
//change this in your blade
route('games_giveaways.show', ['locale' => 'en']);
//to
route('games_giveaways.show', ['locale' => 'en', 'id' => 1]);

How to make auth as user with route id in laravel

I would like to use auth in my login system with two roles:
admin
user.
When I redirect the route to user, it fails.
My Controller:
public function profile($id)
{
$santri = Santri::find($id);
return view('santri.profile', ['santri'=>$santri]);
}
My Route:
Route::group(['middleware' => ['auth', 'checkRole:admin,user']], function () {
Route::get('/santri/{id}/profile', 'SantriController#profile')->name('profiluser');
});
How I check the role:
{
$santri = Santri::all();
if(in_array($request->user()->role,$roles))
{
return $next($request);
}
return redirect()->route('profiluser', $santri);
}
Error:
Missing required parameters for [Route: profiluser] [URI: santri/{id}/profile].
The error message:
Missing required parameters for [Route: profiluser] [URI: santri/{id}/profile].
tells you that you are missing the parameter for this route: profiluser
As you can see here you do not call the route with the correct parameter, you are trying to pass the whole object instead of the id, so instead of this:
return redirect()->route('profiluser', $santri);
Do this:
return redirect()->route('profiluser', $santri->id);
But since you are already passing the whole object you could also do this, lets call it method B.
Here you want to find the model using the passed id:
public function profile($id)
{
$santri = Santri::find($id);
return view('santri.profile', ['santri'=>$santri]);
}
But since you already pass the whole object you could do this:
public function profile(Santri $santri)
{
return view('santri.profile', ['santri' => $santri]);
}
or this, which looks cleaner in my mind:
public function profile(Santri $santri)
{
return view('santri.profile', compact('santri'));
}
You need to pass $santry->id instead of just $santry. Change the line to:
return redirect()->route('profiluser', [$santri->id]);
According to your route:
Route::get('/santri/{id}/profile', 'SantriController#profile')->name('profiluser');
You must pass the user id, like this:
return redirect()->route('profiluser', ['id' => $request->user()->id]);

how to add optional parameter to laravel named routes?

Using Laravel 4.2 and according to routing documentation
We can define a named route as
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController#showProfile'));
And define an optional parameter with this other way
Route::get('user/{name?}', function($name = null)
{
return $name;
});
I want to add an optional parameter to a named route. How to combine both ?
Try this
Route::get('user/{name?}', function($name = null)
{
return $name;
})->name('foo');
Update
sorry the name method not exists in Laravel 4.2
You can do it in another way
Route::get('user/profile/{name?}', array('as' => 'profile', 'uses' => 'UserController#showProfile'))
or
Route::get('user/profile/{name?}', array('as' => 'profile', function($name = null) {
// your code here
})
You can define route common for all functions..
like:
Route::controller('uses', 'UserController');
And define function with optional parameters:
public function getView($param = 0)
//your code here
}
using this you can use optional parameters in a function on which you required.with help of ajax call on function.

Multiple parameter to route error laravel

I have method in my controller (singleProduct):
public function singleProduct($slug)
{
$product= Product::where('slug','=', $slug)->first();
return view('public.product.show')->withProduct($product);
}
And my route is:
Route::get('/{category}/{slug}',['as' => 'single.product', 'uses' => 'LinkController#singleProduct']);
My code in view:
{{$product->title}}
Though i have passed both required parameter for route.My route is returning an error of:
Missing required parameters for [Route: single.product] [URI: {category}/{slug}].
The correct way for defining route params is like:
route('single.product', ['category' => $product->category, 'slug' => $product->slug])
So your route in view will be as:
{{$product->title}}
Docs
In route definition you have slug and category, but in method you get actually just the slug, so maybe adding $category to singleProduct will help:
public function singleProduct($category, $slug)
{
$product= Product::where('slug','=', $slug)->first();
return view('public.product.show')->withProduct($product);
}

NotFoundHttpException - Laravel

i want to make edit-update function..
this is my code :
Admin Controller
public function edit_ist($id_prog)
{
$program_studi = ProgramStudi::find($id_prog);
return view('edit_ist_program_studi',compact('program_studi'));
}
public function update_ist($id_prog)
{
$istUpdate = Request::all();
$program_studi = ProgramStudi::find($id_prog);
$program_studi->update($istUpdate);
return redirect('administrator');
}
Form open in view edit_ist_program_studi
{{ Form::model($program_studi,['method'=>'PATCH','route'=>['update_prodi',$program_studi->id_prog]])}}
Routes:
Route::patch('admin_page/edit_prodi/{id_prog}',
['as' => 'update_prodi', 'uses' => 'AdminController#update_ist']);
But i found error NotFoundHttpException, can you help me to fix this ? thank you
You are missing the GET route to the edit page.
Add something like this:
Route::get('admin_page/edit_prodi/{id_prog}', ['as' => 'edit_prodi', 'uses' => 'AdminController#edit_ist']);

Categories