I have a function in my User model which is called isAdmin, if "Admin" is set to 1 in the database, it returns true, if not it returns false.
How is this gonna work with Auth::user()?
When I do Auth::user()->isAdmin(), it returns "Property [admin] does not exist on this collection instance."
Thats why I came to the conclusion it may not use the User model?
User model
public function isAdmin() {
if($this->admin == 1) {
return true;
} else {
return false;
}
}
public function view ()
{
if(Auth::check() && Auth::user()->isAdmin()) {
$user = User::all();
$post = Post::all();
$visit = Visits::all();
return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
} else {
return redirect()->to('/');
}
}
If I may suggest, for this use case, you can actually make do without an extra function. You could just say auth()->user()->admin, specially if the 'admin' column in the database is boolean type.
Otherwise (even admin coloumn is not boolean type) you can set up a mutator method in the model, like so:
public function getIsAdminAttribute()
{
return (bool) $this->admin;
}
Then to check you can access it like so: Auth::user()->isAdmin or auth()->user()->isAdmin
And better yet, you might want to read about Gate and Policies to achieve more robust access controlling. https://laravel.com/docs/5.7/authorization
Suggestion, change the code to just this:
public function isAdmin() {
return $this->admin;
}
This code does exactly the same as you've got above..
Now in your admin.blade.php you are using:
$user->isAdmin();
But in the controller you have:
$user = User::all();
which returns collection.
You should iterate over it, and check on each user instance if it is an admin:
$users = User::all();
In the view:
#foreach($users as $user)
#if($user->isAdmin())
{{ $user->name }} // some code here..
#endif
#endforeach
No Need To do anything just check if login then auth()->check() is return true then auth()->user() return the user
public function view ()
{
if(auth()->check() && auth()->user()->isAdmin()) {
$user = User::all();
$post = Post::all();
$visit = Visits::all();
return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
} else {
return redirect()->to('/');
}
}
public function isAdmin()
{
return $this->admin;
}
Related
When i dd(realUser) it gives me an integer. And yet it fails to run in the if statement.
Which other way can this code be manipulated?
All I want to do is to verify the id variable passed in the route against the variable in the web guard.
HomeController
public function index(User $user) {
$realUser = Auth::guard('web')->id();
if($realUser == $user){
return view('home', compact('user'));
}
}
dump and die (dd) $user .. $user it's a object..
if($realUser == $user->id){
return view('home', compact('user'));
}
try this one
$user_id= $user->id
if($realUser ==$user_id){
return view('home', compact('user'));
}
Seeking for your help again. Just wondering for any idea's to return something if the column is NULL. Please see below codes: Thank you!
Application Controller
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
If user is not authenticated then you will get unexpected error. So first check the auth as Auth::check()
Try this :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}
I need help with querying the result of relation between models. Two models are defined, User and Role model.
User:
public function roles()
{
return $this->belongsToMany('App\Role', 'role_users');
}
public function hasAccess(array $permissions) : bool
{
foreach ($this->roles as $role) {
if ($role->hasAccess($permissions)) {
return true;
}
}
return false;
}
public function inRole(string $slug)
{
return $this->roles()->where('slug', $slug)->count() == 1;
}
Role:
public function users()
{
return $this->hasMany('App\User', 'role_users');
}
public function hasAccess(array $permissions) : bool
{
foreach ($permissions as $permission) {
if ($this->hasPermission($permission)) {
return true;
}
}
return false;
}
private function hasPermission(string $permission) : bool
{
return $this->permissions[$permission] ?? false;
}
Also, a pivot table called role_users is defined. In the roles database several roles are pre-defined by seeding (Admin, Editor, Author).
I want to query the users by its roles, for example,
$editors = App\User::roles(2)->orderBy('name', 'asc')->get()
where in roles database the id of editor is 2. I got the error
PHP Deprecated: Non-static method App/User::roles()
How to solve this? Note: I'm using Laravel 5.6 and new to Laravel and framework. I tried to refer to docs but it confused me.
Thanks in advance.
You have to use whereHas(..):
$editors = \App\User::whereHas('roles', function ($q) {
$q->where('id', 2);
})->orderBy('name', 'asc')->get()
#devk is correct but here you have another way to get the data of user by role.
\App\Role::where('id', 2)->with('users')->get();
I have two objects out of my User model in Laravel. Let's say
public function someFunction(User $user) {
$adminUser = User::where('role', '=', 1);
if($adminUser === $user) {
return true;
}
return false;
}
Is this the proper way to compare two objects in Laravel? As per PHP Object Comparison, this should work.
Thanks for any input.
Since $user is a User instance, you can do this without executing any additional queries:
public function someFunction(User $user) {
return $user->role === 1;
}
This code will return true if user's role is 1 and false if role is not 1.
You should try this :
public function someFunction(User $user) {
if($user->role === 1) {
return true;
}
return false;
}
hope this work for you !!!