Multiple parameter to route error laravel - php

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);
}

Related

How to show id in Resource Routes url?

Update:
This line of code in the frontend was the culprit:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
I had to change it to:
<inertia-link v-if="options.edit" :href="'/admin/gallery/1/edit'">
to make it comply with the laravel resource format for edit, provided by #Babak.
Original Post:
How would I transform this route in web.php:
Route::get('/admin/gallery/edit/{id}', function ($id) {
$data = Gallery::find($id);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
});
to a resource route with its resource controller function:
Route::resource('/admin/gallery', GalleryController::class);
GalleryController.php:
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Edit:
I've tried both approaches of #Babak's answer, which work for index and create routes but the edit route still throws a 404. It is the only route encompassing an id.
web.php:
Route::resource('/admin/gallery', GalleryController::class)->only('index', 'create', 'edit');
GalleryController.php:
public function edit($gallery)
{
$data = Gallery::find($gallery);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Inertia passes the id from the frontend via href:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
Browser shows:
GET http://127.0.0.1:8000/admin/gallery/edit/1 404 (Not Found)
There is a fixed structure for laravel resource route method, you can see full list here. For edit page, it will generate something like '/admin/gallery/{gallery}/edit'
You can write it like below:
In your web.php file:
Route::resource('/admin/gallery', GalleryController::class)->only('edit');
And in your controller, name of the resource must be the same as your function's parameter.
public function edit($gallery)
{
$data = Gallery::find($gallery);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Or, you can customize it using parameter method. Refer to here
Route::resource('/admin/gallery', GalleryController::class)->only('edit')->parameters([
'gallery' => 'id'
]);
And your controller
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}

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]);

Laravel set value in get route

I have one route which is accepting one argument perfectly as
Route::get('view-request/type/{type}/id/{id}', 'CustomerReqController#testing')->name('request.manage');
and also call it in blade by this
<a href="{{route('request.manage',['type'=>'new','id'=>'data'])}}"
and the controller is
public function testing(Request $request,$type,$id){
dd($request->all());
}
it gives me error
Missing required parameters for [Route: request.manage] [URI: admin/view-request/type/{type}/id/{id}]. (View: /var/www/html/ehs_crm_laravel/resources/views/common/navbar.blade.php) (View: /var/www/html/ehs_crm_laravel/resources/views/common/navbar.blade.php) (View: /var/www/html/ehs_crm_laravel/resources/views/common/navbar.blade.php)
What am i doing wrong?
use:
<a href="{{ route('request.manage', ['type' => 'new', 'id' => 'data']) }}">
you can get your route parameter by simply using this code. hope this will work for you. for Get and Post method both.
public function testing(Request $request)
{
$type= $request->type;
$id= $request->id;
}
follow steps this code is working to me.
1 : declare route
Route::get('view-request/type/{type}/id/{id}', 'UserController#index')->name('request.manage');
2: create link
Register
3: get data in controller
public function index($type,$id,Request $request){
echo $type;
echo $id;
}

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

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

Laravel Passing Additional Parameter To Controller

I need to pass an additional parameter($uid) from my index.blade.php to my edit.blade.php by clicking on a button.
My index.blade.php:
Edit
My FlyersController:
public function edit($id, $uid)
{
return view('backend.flyers.edit')->withUid($uid);
}
With the code above I get an error: "Missing argument 2 for App\Http\Controllers\FlyersController::edit()"
What am I doing wrong here?
The error is not throwing from the action method. It is coming from route for that URL.
Check the URL for passing argument to the the controller.
If this is the your desire URL localhost:8000/backend/flyers/10/edit?%24uid=1 then the second argument is in $request variable not in controller function argument.
You should pass an array into action() helper:
action('FlyersController#edit', ['id' => Auth::user()->id, 'uid' => 1])
Ok,
the only way I can solve this is by using the following in My FlyersController:
public function edit(Request $request, $id)
{
return view('backend.flyers.edit')->withRequest($request);
}
and access then the uid with {{request->uid}} in my view.
If anybody has a better solution for this, let me know.
Use this code
return view('backend.flyers.edit', ['var1' => $var1, 'var2' => $var2]);
That will pass two or more variables to your view

Categories