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

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

Related

Making Routes in Laravel from URL

If our URL is http://127.0.0.1:8000/student/submit-details/1234 then its Route will be:
Route::get('student/submit-details/{id}',
'studentController#submitDetails')->name('submitDetails');
What will be the route if the URL is following?
http://127.0.0.1:8000/student/submit-details?code=1234
I'm using the following Route, but it is not picking it and not working. Does anyone know what will be its Route? I went through the documentation and found no help there.
Route::get('student/submit-details?code={id}', 'MyController#submitDetails');
Your route should be look like as:
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
http://127.0.0.1:8000/student/submit-details?code=1234
in above URL string after the question mark is query parameter and get the value of the query parameter in the controller you should use $_GET:
$_GET['code']
The placeholder parameters for routes are only specified for Route parameters but rather for query parameters. The Route should be only
Route::get('student/submit-details', 'MyController#submitDetails');
You can access the value in the controller via Request instance
public function submitDetails(Request $request) {
dd($request->code);
}
Try this
http://127.0.0.1:8000/student/submit-details?code=1234
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
You have to use get method
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
In Laravel if you want to pass data with GET method :
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
It will give you output like this :
http://127.0.0.1:8000/student/submit-details?code=1234
If you have multiple parameter, it will like :
http://127.0.0.1:8000/student/submit-details?code=1234&code2=5678
You can access the parameter from controller like this :
public function edit(Request $request){
$code = $request->input('code');
dd($code); // 1234
}
Take a look at the $_GET and $_REQUEST superglobals.
If you want route
http://127.0.0.1:8000/student/submit-details?code=1234
Route route will be
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
And usage
route('submitBankDetails', ['code' => 1234])

How to pass a variable as a route parameter when accessing the route in 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).

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

How do I redirect to a URL with query parameters?

I am trying to do a redirect with query parameters, using the redirect() helper:
$link = 'https://example.com' . '?key1=value1&key2=value2';
return redirect()->to($link);
The problem is that when the $link is passed to the to() method Laravel removes the question mark leading the query string, so it turns this:
https://example.com?key1=value1&key2=value2
into this:
https://example.comkey1=value1&key2=value2
(again, notice the missing ? in the final link).
How do I make a redirect with query params appended to a custom URL?
Use:
return redirect($link);
If you want to redirect a named route with query string, use:
return redirect()->route('route_name',['key'=> $value]);
Documentation
The approved answer explains it all, according to the documentation. However, if you are still interested in finding some kind of "hard-coded" alternative:
$link = "https://example.com?key1={$value1}&key2={$value2}";
Then,
return redirect($link);
Reference
If the link is to a page on your domain, you don't need to re-write the domain name, just:
$link = "?key1=${value1}&key2=${value2}";
Laravel will automatically prepend the URL with your APP_URL (.env)
If you're building a Single Page Application (SPA) and you want to redirect to a specific page within your app from a server request, you can use the query method from the Arr helper class. Here is an example:
$result = Arr::query([
'result' => 'success',
'code' => '200'
]);
return redirect("/purchase?$result");
This will redirect the user to the /purchase page with the query parameters result=success and code=200.
For example, the final url would be:
http://example.com/purchase?result=success&code=200

Get part of url from Laravel named route

Is there a possibility to get the part of url, that is defined in route?
For example with this route:
Route::get('/editor/{id}', 'EditorController#editor')->name('editorNew');
after using mentioned functionality, let's say route_link(); i would like to get:
$route_link = route_link('editorNew', array('id' => 1));
//$route_link containts "/editor/1"
I tried to use route(), but i got http://localhost/app/public/editor-new/1 instead of /editor-new/1 and that's not what i wanted.
For clarity need this functionality to generate links depending on machine, that the app is fired on (integration with Shopify).
You can use route method to get the relative path by passing false in the third parameter as:
route('editorNew', [1], false); // returns '/editor-new/1'
You could use the following:
$route_link = route('editorNew', [1]);
1 is the first value that will be on the route, at this moment {id}.
If you want to use the paramater (id) in your method, it will be the following:
public function editor($id) {
//your code
}
And in the view you could use:
Route::input('id');
Hope this works!

Resources