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.
Related
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 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
I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
I'm trying to make a "Profile settings" section in an application.
The thing is, I learned how to do this the "Admin" way, the route would be /users/{user}/edit, the would call the edit method on the controller and it would return the edit view. There I would have a form which the user would patch to the route users/{user} and it would call the update method on the controller.
But I don't want anyone editing other users, so I'd like to know if there's a way to limit this route to the current user only.
Thanks in advance.
Since version 5.1 Laravel has Policies which are exactly what you need.
You can create a new policy by typing in command:
php artisan make:policy UserPolicy
In your UserPolicy class you can include the following method:
public function updateProfile(User $user, User $updatedUser) {
return $user->id === $updatedUser->id;
}
Please note: The first parameter $user is resolved automatically behind the scenes and is the currently logged in user. When checking the policy through the Gate facade in your application you need to pass only the second parameter $updatedUser.
Then you need to register your policy in the AuthServiceProvider:
use Acme\User;
use Acme\Policies\UserPolicy;
...
class AuthServiceProvider extends ServiceProvider {
protected $policies = [
User::class => UserPolicy::class
]
Now when you have your policy registered you can check it across your app using the Gate facade like so:
if(Gate::allows('updateProfile', $user)) {
// Your logic goes here
}
Or the other approach with I like more using the denies method and include it at the beginning of my controller methods and return http error:
public function edit($id) {
if(Gate::denies('updateProfile', $user)) {
abort(403, 'You do not have permissions to access this page!');
}
// The check is passed and your can include your logic
}
You can also check for permissions in your blade files using can and cannot like so:
#can('updateProfile', $user)
// Show something only to the user that can edit the $user's profile
#endcan
For more info check the docs.
you should not need to pass in the user id as there user is already logged in and there for should be able to edit themselves, thus only targetting the logged in user.
So you can use the routes /user/editand /user/updateetc
Just get the current user details like
Auth::user()->id
or something else like
$user = Auth::user();
Thus only the logged in user (themselves) can be edited.
In the view there should be a button or link, on click pass the ID to the desired route that's it.
Example:
For Grabbing the current logged in User id you should do like
$user = Auth::user()->id;
And directly in the route you can get it like
Edit
Now when someone clicks on the Edit Button/Link, the route will look like route/currentuserid.
I just started learning Laravel 5 and came across a problem i could't solve:
How to tell that a certain user can only see certain things.
For example if i view a profile which is not my own, the "Edit Profile" button should not be visible. But if i look at my own profile this button should be visible.
What i already got is to authorize certain requests. For example to authorize a user to actually update a profile:
public function updateProfile(Profile $profile, UpdateProfile $request){
//update the given profile
}
So UpdateProfile is here a Request Class which has a authorize() and a rule() method and in the authorize() Method i check if the logged User is updating his own profile.
So i thought maybe i can use the authorize() method on its own, but i am not really sure how.
Now of course i could always check sth like:
if($user -> userID == Auth::user() -> userID)
But what if i need to check sth more complex, for example when i write a post and want to show a delete button for that post i want to check:
Is the user admin, if not is the user writer of that post, if any of this is true, show delete button.
So my question would be, where would i check sth like this in laravel 5?
You could write a userCanEdit method on your Post class. Something like this:
function userCanEdit(User $user)
{
return $user->isAdmin() || $this->user_id == $user->id;
}
And then just call it on your view:
#if ($post->userCanEdit(Auth::user()))
Edit
#endif
The advantage of this is that you keep your view clean and centralize the business logic in a single, reusable, method. If the definitions for a user who can edit a post ever change, that is the only place you'll have to worry about.
So my question would be, where would i check sth like this in laravel 5?
In your views. For example, let's assume that you are loading a blog content like this:
// controller
public function showPost(Post $post){
return view('views.post', ['post' => $post]);
}
In the view, we want that only the author make changes to it.
// post view
<h2>{{ $post->getTitle() }}</h2>
#if ($post->getAuthor()->getId() === Auth::user()->getId())
Edit
#endif
As you can see above, if the author is the same user as the authenticated, then she/he can see an editing link for this post.