So, I have the following Tables named:
ALBUMS(id, name, shared, group_id, user_id)
GROUPS(id, name, user_id)
GROUP_USER(group_id, user_id)
What I want to do is, using Laravel Eloquent to get the results of a query by checking if the AUTHENTICATED user is a member of the GROUP owned by the ALBUM.
Album Model:
public function groupSecurity()
{
return $this->belongsTo(Group::class, 'group_id');
}
Group Model:
public function ownedBy()
{
return $this->belongsTo(User::class, 'user_id');
}
public function members()
{
return $this->belongsToMany(User::class, 'group_user');
}
Thank you for your help :)
You can do like this:
$album = Album::whereHas('groupSecurity', function ($query) {
$query->whereHas('members', function($query2){
$query2->where('user_id', auth()->user()->id);
})->where('user_id', auth()->user()->id);
})->first();
So you search for one album that has a groupSecurity which has in his members the auth user and also it's owned by the auth user.
If a record match this conditions $album will have an instance of Album model, if not it will be null so you can do:
if($album){
// AUTHENTICATED user is a member of the GROUP owned by the ALBUM
}
Related
I have 4 tables in project's database:
users (id, name)
teams (id, name)
team_members (id, user_id, team_id)
team_member_permissions (team_member_id, team_id)
I need to get user's teams. My current solution is to create teamMembers relationship in User model:
public function teamMembers()
{
return $this->hasMany(TeamMember::class);
}
members relationship in Team model:
public function members()
{
return $this->hasMany(TeamMember::class);
}
permissions relationship in TeamMember model:
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
But how can I optimise it? For example, to get all teams for users I need to use
Team::whereHas('teamMember', function($query) use ($user){
$query->where('user_id', $user->id);
})->paginate();
But how can I implement a relationship that will contains all user's teams to get user's teams in one string of code, like $user->teams?
I suggest you use many to many relation in regards to your team_members tables to explicitly mention as pivot.
User model:
public function teams()
{
return $this->belongsToMany(Team::class, 'team_members');
}
Team model:
public function users()
{
return $this->belongsToMany(User::class, 'team_members');
}
Since you want to use whereHas clause on teamMember table, I believe that you want to extract teams based on the fact that all teams belonging to that user based on team_members table.
So, You can specify the table name on many to many relation as above.
Now you can use
$user->teams
I have two tables, customers and users in users table I have:
id
name
email
password
in customers I have
id
customer_id
package_id
when someone register, in user table create id number and in the same time it creates in customers table: id and customer_id get the same value from id->user
inside customers model I have
public function owners()
{
return $this->belongsToMany('App\User', 'customers');
}
all I need now just to check after registration if package_id for the current customer_id is empty or null return to.. else if it has any number return to...
I tried this in controller but doesn't work
public function redirectpackage(Request $request)
{
if (Auth::check() && Auth::user()->customer->package_id == 1) {
return view('index.customer.customerpackage');
}
else{
return view('index.customer.customerabout');
}
}
If customers.customer_id is a foreign key that references users.id, then:
A customer can only have one user ("belongs to").
But a user can have many customers ("has many").
So, inside owners() replace belongsToMany() with belongsTo():
public function owners()
{
return $this->belongsTo('App\User', 'customer_id');
}
And inside the User model, add a relationship to the Customer model:
class User extends Model
{
public function customers()
{
return $this->hasMany('App\Customer', 'customer_id');
}
}
Now you can check if a user has a given package_id in the customers() relationship:
Auth::user()->customers()->where('package_id', 1)->count();
Alternatively, you can use isEmpty() instead of count().
I have a website build in Laravel.
I have two tables - Groups and Group members.
For each group_member, the row in the table has id, group_id and user_id.
The groups have a name and a description.
When a user joins a group, a row is created in the group_member table.
But I now need to get the groups that a user is part of.
So if I have user_id = 5, I need to get all the rows in group_member where user_id = 5, and then get the corresponding group, so I can query the groups.
I need to do something like $groups = Groups::whereGroup_member ...
But I cant query the model like that, because in Groups there is no where it specificies who the members are, it is just the group details - the members are specificed in group_member table.
How do I get the groups, which a member is part of using the laravel query standards?
In your User.php Model
public function group_member(){
return $this->hasMany(GroupMember::class,'user_id','id;);
}
In your GroupMember.php Model
public function group(){
return $this->belongsTo(Group::class,'group_id','id');
}
Your query will be
$users = User::with('group_member.group')->find($user_id);
You should use Many-to-Many relation:
class Group
...
public function members() {
return $this->belongsToMany(User::class); // Users (members) that belongs to Group
}
class User
...
public function groups() {
return $this->belongsToMany(Group::class, 'group_member', 'user_id', 'group_id'); // Groups that User belongs to
}
And at controller, when You have user id:
$groups = User::where('id', $user_id)->groups;
More about this written at official docs: https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
I have 2 tables: USERS and SUBJECTS
The relationship between USER and SUBJECT is many to many.
In the User.php and Subject.php models, I defined:
User.php
function subjects() { return $this->belongsToMany('App\User'); }
Subject.php
function users() { return $this->belongsToMany('App\Subject'); }
The pivot table is subject_user and it has 3 columns:
subject_id, user_id, finished
The finished value can only be between 0 and 1.
Now I know that when I want to select all the subjects that an user studied, I have to write $user->subjects.
But what if I want to select all the subjects that an user studied and the finished value in the pivot table is equal to 1?
You need to add "withPivot()" to your relationship definitions, like this:
function subjects() { return $this->belongsToMany('App\User')->withPivot('finished'); }
function users() { return $this->belongsToMany('App\Subject')->withPivot('finished'); }
Then you can do:
$user->subjects()->where('finished', 1)->get();
You will need to eager load that relationship and use the wherePivot method.
$user = User::with(['subjects' => function($q) {
$q->wherePivot('finished', 1);
}])->fine($user_id);
I got a many to many user and role structure
users
id
name
roles
id
name
role_user
user_id
role_id
Model
User.php
public function roles() {
return $this->belongsToMany('Role');
}
Role.php
public function users() {
return $this->belongsToMany('User');
}
There are two data admins and members in roles table, I would like to know to to filter users which role is admins.
This should give you all users who are admins.
$users = User::whereHas('roles', function($q) {
$q->where('name', '=', 'admins');
})->get();
You can see more information on the has() method at http://laravel.com/docs/eloquent#querying-relations