I am having trouble creating user roles and permissions in my system.
I'm using Package acl / laravel, and documentation describes the stages. I wrote the following code UserController before the Get User Roles:
Documentation: https://github.com/kodeine/laravel-acl/wiki/Create-Roles
$users = User::all();
$users->getRoles();
return view('users.index', compact ('users'));
Error:
BadMethodCallException in Macroable.php line 81:
Method getRoles does not exist.
What is going wrong, and how can I fix it?
The problem is that getRoles method exists for User object and when you use User:all() in $users variable you will get all users and not the single one.
So you can do:
$users = User::all();
return view('users.index', compact ('users'));
and in your Blade template you can in foreach loop user getRoles() method for single user and display them
Related
I made an application using vue laravel and inertia, I'm trying to feed my application through the $page that inertia makes available, when I went to test in the console.log the $page.props.user works normally showing the array with all the data, but how much I try to call a property of the base, for example the name, I put $page.props.user.name it gives undefined, if you can help me thank you.
mounted(){
console.log(this.$page.props.user)
console.log(this.$page.props.user.name)
};
Controller:
public function home(){
$user = User::all();
return Inertia::render('Home', [ 'user' => $user]);
}
$user = User::all();
This query returns all users as an array (a Laravel collection in fact). So the view will obviously receive an array as well.
You likely need Auth::user() (using the Auth facade) or whatever equivalent you have to retrieve the currently logged in user.
I want to add additional data to $user collection so can get the profile fields in the view
I have tried using $user['profileFields'] = $this->getProfileFields() and pass $user to the view using compatct or with and this is working fine.
DD
However, I have found some reference over the net saying I can extend using map but when I tried it is giving the following error
BadMethodCallException
Call to undefined method App\User::map()
So here is what I am trying to understand
Question:
Is the below code is wrong and won't work for what I am looking for and the first approach is the solution? Is there any
recommended method to add additional data to the $user collection?
public function show(User $user)
{
$user->map(function ($user){
$user['profileFields'] = $this->getProfileFields();
return $user;
});
return view('admin.user.show', compact('user'));
}
collection methods are works on the collection, here you're getting the object of user.
$user->profileFields = $user->getProfileFields();
return view('admin.user.show', compact('user'));
If the profileFields is in another table and having a foreign key with model ProfileField, then try to add a one to one relation to the User model.
Inside the User model, add a function
public function profileFields()
{
return $this->belongsTo('App\ProfileField', 'foreign_key','other_key');
}
This will give you the profileFields in every user when calling using eloquent.
I'm very confused by this!
In a laravel controller:
$user = Auth::user();
return $user;
returns the entire user object
$user = Auth::user()->id;
return $user;
returns the id of $user as expected
however if I put that exact thing into a query such as:
$user = Auth::user();
$query = Model::where('user_id', $user->id)->get();
return $query;
I get an error that I'm trying to get an 'id' property of a non-object!!!
How can this be possible?
I've also checked 100 times that I'm logged in when testing this.
Editing to add that I've also tried:
$query = Model::where('user_id', 1)->get();
and that works fine
edit to show the function:
$user = Auth::id();
$result = Lesson::where('user_id', $user)
->whereNotNull('notes')
->get();
return response()->json($result, 200);
Expected results: A list of objects with a filled in "notes" column.
Actual results: an empty [] and an error saying can't get id of a non-object
Don't use Auth in Models.
You can set a relation between User and your Model and do query like:
Auth::user()->relation()->get();
This error occurs if the Model doesn't contain any raw with that user id. Make sure you have the raw in the Model where you are querying to get data by user id.
You should try this
$user = Auth::user();
$result = Model::where('user_id', $user['id'])->get();
The error occurs because of you passed property of non-object.
Even after error not solve then restart your xampp or wamp.
If possible, upload the screenshot of error.
Your code should work if you are authed, it seems your are not based on the error message. Since you return json, i assume this is an api request. Api-routes uses another auth driver as default in Laravel (token vs. session). Either change the driver/guard or move your route(s) from routes/api.php to routes/web.php.
See docs for auth
You can change the driver in config/auth.php.
try this
$user_id = Auth::user()->id;
$query = Model::where('user_id', $user_id)->get();
return $query;
I have table with many to many relationship.
User many to many Permission
I already define many to many relationship on both model, and create the pivot table also.
What I want is get all user which contain permission name
What I have done so far is
User::all()->permissions->contains('name', 'access.backend.admin')->get();
But it give me
Undefined property: Illuminate\Database\Eloquent\Collection::$permissions on line 1
What wrong with my code?
User::All() returns a collection not model object. You have iterate over the collection to get the model object and use ->permissions().
For exapmle:
$users = User::all();
foreach ($users as $user) {
$user->permissions->contains('name', 'access.backend.admin'); // returns boolean
}
Or you can get a single model from DB as:
$user = User::first();
$user->permissions->contains('name', 'access.backend.admin'); // returns boolean
Update 1
To get users which contain desired permission use filter() method as:
$filtered_users = $users->filter(function ($user) {
if ($user->permissions->contains('name', 'access.backend.admin')) {
return $user;
}
});
Update 2
You can also write a query which returns the desired result as:
$filtered_users = User::whereHas('permissions', function($q) {
$q->where('name', 'access.backend.admin');
})->get()
I have a similar case of questions and tags, they have many to many relationship.
So when i have to fetch all question with a particular tag then i do this
$tag = Tag::where('name','laravel')->get()->first();
I first retrieved the Tag model with name laravel.
and then retrieved all questions having tag laravel.
$questions = $tag->questions;
Similarly you can do this
$permission = Permission::where('name','access.backend.admin')->get()->first();
$users = $permission->users;
When the below is executed:
$user = User::where('email','=',$email)->take(1)->get();
Auth::login($user);
I get the error:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, instance of Illuminate\Database\Eloquent\Collection given ...
I have tried changing this to:
$user = User::where('email','=',$email)->take(1)->get();
$userid = $user->id;
$user=User::find($userid);
Auth::login($user);
But then I get the error:
Undefined property: Illuminate\Database\Eloquent\Collection::$id
when you are using get() it returns collection of users means you may have multiple records though you have used take(1) but that number may be different so it always returns collections, so you can`t directy access property of object.
you can use first() to get single user object.
try this
$user = User::where('email','=',$email)->first();
Auth::login($user);
EDIT
you can use firstOrFail() instead of first() here, as if you don't get specified user firstOrFail() through an error.