How to pass a variable as a route parameter when accessing the route in Laravel? - laravel

This does not work:
return redirect('/view-project-team/' . $projectRequest->id );
What is the right way to pass a variable into the route in this context?

As was said in comments you should use name of the route:
return redirect()->route('view.project.team', ['id' => $projectRequest->id]);
Names can be defined in your router:
Route::get('/view-project-team/{id}', 'YourController#yourHandler')->name('view.project.team');
Note that:
Dots in name of the route are not necessary (you could give any name).
'id' in route() call is refer to {id} in Route::get() call (names must match).

Related

How to find the parameter name expected in laravel model route binding

I have a route like this
Route::get('/vcs-integrations/{vcs-provider}/authenticate','VcsIntegrationsController#authenticate');
and the method to handle the route I am using the model route binding to happen is as follows
<?php
....
use App\Models\VcsProvider;
....
class VcsIntegrationsController extends Controller
{
public function authenticate(VcsProvider $vcsProvider, Request $request)
{
...
// some logic
...
}
}
when I try to access the route I am getting 404 due to the parameter name is not matching.
So, how do I know the parameter name expected by laravel in route model binding ?
From the route parameter docs:
"Route parameters are always encased within {} braces and should consist of alphabetic characters, and may not contain a - character. Instead of using the - character, use an underscore (_)." - Laravel 7.x Docs - Routing - Route Parameters - Required Parameters
You should be able to define the parameter as {vcs_provider} in the route definition and then use $vcs_provider for the parameter name in the method signature if you would like. You can also name it not using the _ if you prefer, but just avoid the -, hyphen, when naming.
Have fun and good luck.
Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.
This means that if you want implicit binding to work, you need to name the route param same as your variable. Since your variable's name is vcsProvider, your route should be:
Route::get('/vcs-integrations/{vcsProvider}/authenticate','VcsIntegrationsController#authenticate');

laravel: Route [dashboard] not defined

when admin login then he redirects to dashboard page but by clicking on its main button(like home button) it shows error, i added this code: <a href="{{route('dashboard')}}"> in it but it says:
Route [dashboard] not defined
this is my route:
Route::get('/admin/dashboard','AdminController#dashboard');
any solution to resolve this issue
Give the name to Route.
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard);
or
Route::get('/admin/dashboard',[
'uses' => 'AdminController#dashboard',
'as' => 'dashboard']);
As shown in the docs:
route()
The route function generates a URL for the given named route:
$url = route('routeName');
as we see, it generates a URL for the given named route, so you have to provide a name for your route like the following from docs too:
Named Routes
Named routes allow the convenient generation of URLs or
redirects for specific routes. You may specify a name for a route by
chaining the name method onto the route definition:
Route::get('user/profile', function () {
// })->name('profile');
You may also specify route names for controller actions:
Route::get('user/profile', 'UserProfileController#show')->name('profile');
So add a name to your route:
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard');
Hope it helps

Is it possible to get the url without the domain name /localhost name or the

The following route:
route('schools.student',$user->id);
will return:
localhost:8402/api/v1/schools/student/3
Is it possible to just return the url without localhost:8402?
You can pass a third parameter to the route() helper for relative urls.
route('schools.student', $user->id, false);

Change default URL param name when using Route::resource

When using Route::resource(), Laravel of course 'chooses' somethings for you, such as route names and methods.
I know how to override, for example, the allowed routes/methods:
Route::resource('user', 'UserController', array('only' => array('index', 'show')));
But I now need to override the URL param name that Laravel sets for the user routes. By default, in the example above, it will be user. But, I want it to be user_id.
Does Laravel provide a way to set this, when using Route::resource?
So that, for example, I would end up with the route:
mydomain.com/users/{user_id}
rather than:
mydomain.com/users/{user}
Thanks
Found it. Yes, Laravel does provide a way to override this when using Route::resource().
Route::resource('users', 'UserController')->parameters([
'users' => 'user_id'
]);
The key of the element in the array in the argument to 'parameters' is the same as what you enter as the first argument of the 'resource' method (not the 'singular' version or something)

Laravel form open with multiple parameters

I am trying to pass variables with form open with following code:
{{ Form::open(['method' => 'PATCH','route' => ['note.update','project','1','1']]) }}
Here is my NoteController.php file
Class NoteController extends BaseController{
public function update($belongs_to,$unique_id=0,$note_id=0){
return $unique_id;
}
}
routes.php file is
Route::resource('note', 'NoteController');
Why I am only able to access $belongs_to variable and $unique_id and $note_Id are always 0 as given as default value??
That's because the routes registered with Route::resource only take one url parameter.
Take a look a this
So what you need to do is use this route:
Route::patch('note/{belongs_to}/{unique_id?}/{note_id?}', 'NoteController#update');
If you want to keep the other routes from Route::resource just add it before Route::resource
Route::patch('note/{belongs_to}/{unique_id?}/{note_id?}', 'NoteController#update');
Route::resource('note', 'NoteController');
If you don't want to add the route like this, you'll have to use query parameters to pass additional information

Resources