I want to create dynamic route on my navbar and redirect it to http://localhost:8000/tasks/nenad page
Web.php
Route::get('/tasks/{first_name}', 'Viewercontroller#profile')
->middleware('viewer')
->name('profile');
ViewerController
public function profile($first_name) {
$user = User::whereFirst_name($first_name)->first();
return view('viewers/tasks', compact('user'));
}
Navbar.blade.php:
<li>Tasks</li>
I know that i have to change link on my navbar page but doesn't know how, any help will be great...
it won't work because your route expects a parameter, so you have to indicate on your navbar.blade.php the parameter you want to pass.
For example I'll assume you have a user saved on $user, then you would do this:
route('profile', ['first_name' => $user->name]);
You can read more about URL generation on Laravel docs
Related
I have simple URL /players/USERNAME to view user profile. I want to insert some thing only in user profile page. Cuz user profile got another links.
Example:
#if (Request::is('players/{{ $user->slug }}'))
#include('users::stats')
#endif
But dont get anything. Any ideas what's wrong? Thanks!
I don't think Request::is is necessary in your blade view. You should have a route defined in your web.php file to capture that pattern meaning you already know the end user is on players/{username}.
web.php
Route::get('/players/{slug}', [
PlayerController::class, 'show'
])->name('players.show');
PlayerController.php
public function show($slug)
{
$user = User::where('username', $slug')->firstOrFail();
return view('players.show', compact('user'));
}
Blade view (players/show.blade.php)
// some stuff
#include('users.stats')
// some other stuff
Note that I've named the methods and files based on what you've provided, you'll need to change them to be whatever they are in your project.
I've a Register link on menu bar. On register page I have 2 forms, Login and Signup.
By default, it shows the Login form but I want it to show the Signup form first.
I thought If I do it with route by giving an Id to my signup form then I would be able to do that.
But it's giving me the error by saying
Missing required parameters for [Route: getRegister]
How would I modify my route?
Route
Route::get('/registration{url}', [
'uses' => 'niceActionController#getRegister',
'as' => 'getRegister'
]);
Controller
public function getRegister($url)
{
return view( '/' . $url, ['url' => $url]);
}
Header
{{ __('Register')}}
don't use the #url parameter in your web routes and controller, it is not meant for the backend.
Route::get('registration', ['niceActionController::class, 'getRegister'])->name('getRegister');
{{route('getRegister')}} will print out the link to the page, you'll want to put you parameter after the route, it will be handled after your page has loaded.
{{ __('Register')}}
in your controller you put the name of your blade template (not a route)
public function getRegister()
{
return view('nameofthetemplate');
}
Define your routes as below.
Route::get('action/{action_type}', 'NiceActionController#action') ->where('action_type', 'login|register') ->name("action");
In the view you can get route as below
{{route('action', ['action_type' => 'register'])}}
OR
{{route('action', ['action_type' => 'login'])}}
In the controller you can access the action_type parameter value as below.
$request->action_type;
Make sure you add Request $request parameter in method
Note:
The hash value is not accessible by the server script. You can either append action type in URL as shown in the above sample or include it in the query string. Check below link check how to get query string parameter value.
https://laravel.com/docs/8.x/requests#accessing-the-request
Tip: Please try to follow the Laravel naming conventions.
Because # symbol isn't parsed by the server, it's used by the browser for on-page navigation.
You might be familar with CSS/JS. # links in URLs will take you to either a named tag or ID tag in the page (e.g. http://page.com/#tag will take you to an element with the ID "tag")
When you hit http://page.com/registration/#register_forms the browser hit the page http://page.com/registration/ and searching for the id="register_forms" on your HTML.
Use this as your route
{{ __('Register')}}
Route::get('/registration/{url}', [
'uses' => 'niceActionController#getRegister',
'as' => 'getRegister'
]);
public function getRegister($url)
{
return view( viewNameHere, ['url' => $url]);
}
I'm getting a 404 page not found when trying to use the 'users/{id}' route, the route
leads to the ProfilesController#edit method
Profiles Controller:
public function edit($id){
$user = User::findOrFail($id);
return view('profiles.edit', compact('user'));
}
here is the routes in my web.php;
Route::get('/users/{id}', 'ProfilesController#edit')->name('user.edit');
Route::put('/users/{id}/update', 'ProfilesController#update')->name('user.update');
and i have a edit.blade file in my profiles folder
Change route to this, the call users/{id} instead of profile/{id}. Otherwise you can stick with your approach by using the second option below.
Route::get('/users/{id}/edit', 'ProfilesController#edit')->name('user.edit');
Route::put('/users/{id}', 'ProfilesController#update')->name('user.update');
Also you are trying to access the route profile/{id} but which for my undestanding it is not defined. You either will have to change your route to:
Route::get('/profile/{id}', 'ProfilesController#edit')->name('user.edit');
Route::put('/profile/{id}/update', 'ProfilesController#update')->name('user.update');
I have a route which redirects to a controller which tells view to show. But how can I go to a particular part of the page. Let me Explain:
I am using fullpage.js library to create a page breakpoints.
for example: /home#firstPage , /home#secondPage and so on. When I am redirected to /home I would like to go to a particular section of the page. All I need is a way to enter #3rdPage in url.
Where and How do I put this to see it action?
my Routes
Route::get('/home_done', ['as' => 'home_done','uses' => 'HomeController#home']);
my Controller
public function home()
{
return view('home_done');
}
I would like to do something like:
return view('home_done#3rdPage');
You should setup a new function and route to redirect the users to correct page after executing your logic:
public function home()
{
return view('home_done');
}
public function step_3()
{
// do something and redirect
return redirect('home_done#3rdPage');
}
Using view don't work since you are loading a file, not the page.
You need change your behavior in JS, when a user comes to your page without a # consider it equal of #firstpage.
and in your html manage all over pages by properly generate urls with #what_you_need.
in your blade example:
firstpage
//OR
firstpage //because this is the same for your JS
secondpage
or blade syntax
firstpage
//OR
firstpage //because this is the same for your JS
secondpage
I created a dynamic page by taking data from the database.
Example: example.com/post/SE12
This /SE12 is dynamically created. Now I have a button in this page where I want to edit the post.
example.com/post/SE12/edit.
How can I link a button to this page using laravel? and how can I route this page and call it in controller?
In route.php
Route::resource('posts','PostsController');
Route::get('/posts/{code}', [ 'as'=>'post-show', 'uses'=>'PostsController#show']);
routes.php:
Route::get('post/{code}/edit', [ 'as'=>'post-edit', 'uses'=>'PostsController#edit']);
Controller:
public function edit($id)
{
echo $id; //this will give the code. e.g. SE12
}
View:
some title
N.B. you are resource controller and manual mapping at the same time. stick to either one as much as possible. otherwise routes will get conflict.