I have users table. (User Model & Controller)
each user can start a fight. the fight table contains the user_id. (I already have Fight Model & Controller)
Once the fight has finished, the record is deleted from the fight table.
The question:
What's the right relationship between users and fight so I can access the user fight within user->fight?
How I can check using Laravel to make sure the user has no fight in the fight table before allowing him to create a new one? (SELECT * FROM fight WHERE user_id = USER_ID)
The correct relationship on the user side is:
$this->hasMany(Flight::class);
Also about your second question, there is an 'has' method that can query a relationship for existance.
So in your case:
$user->has('flight');
Reference: https://laravel.com/docs/5.2/eloquent-relationships#querying-relations
Related
I have a (skills) table and (users) table, and I have created (skill_user) table as a pivot,
but my problem is how can I let the user insert any skills he wants, and at the same time it is many to many relationships, which means the user should select the skills which already exist? i cannot understand how can I fix this issue?
There are 3 methods to work with many-to-many relationships:
attach
detach
sync.
I would say sync is more suitable for your case: user selects a list of skills, and you save only these. This also allows to remove skills if user unselected one.
I'm using Laravel 5.4 and have a model and table structure as follows:
users
-----
id
accounts
--------
id
holdings
--------
id
account_id (foreign key to account)
user_accounts
-------------
id
user_id
account_id
A user can have multiple accounts
An account can be shared by multiple users
Each account has multiple holdings
A user therefore indirectly has many holdings through their many accounts.
I need help to define a relation on the User model called "holdings" to get me all the holdings applicable to the user (based on the accounts they are linked to).
I've tried lots of different things and spent ages on google. I can get close with both belongsToMany and hasManyThrough, but they only seem to work for 3 table relationships where the intermediate table stores primary keys from the other tables. I can reduce my relationship to 3 tables (rather than 4) if I make use of the account_id foreign key on the holdings table to remove the need to join through the accounts table, however I can't seem to get this to work.
belongsToMany - holdings.id needs to be holdings.account_id:
select * from `holdings`
inner join `user_accounts` on `holdings`.`id` = `user_accounts`.`account_id`
where `user_accounts`.`user_id` = ?
hasManyThrough - user_accounts.id needs to be user_accounts.account_id:
select * from `holdings`
inner join `user_accounts` on `user_accounts`.`id` = `holdings`.`account_id`
where `user_accounts`.`user_id` = ?"
Well it's not strictly an answer as I was asking about laravel 5.4, but it is a possible solution.
It turns out I'm in luck as I came across this Laravel belongsToMany relationship defining local key on both tables which indicates the belongsToMany relationship has been enhanced in Laravel 5.5 to support this type of scenario.
I have upgraded my project to L5.5 and replaced my hack with the relation:
return $this->belongsToMany(Holding::class, 'user_accounts', 'user_id', 'account_id', null, 'account_id');
And am happy to say it seems to work perfectly - at least for basic gets which is my use case, I haven't tried any updates etc.
Thanks to #cyberfly and those who posted the answer above
I have a users table
id
role_id
"other standard auth stuff"
I also have a roles table
id
role
I do not want to change the users table in the sense that its currently doing all the auth stuff provided with laravel 5.4 but
I have users with a role. Let's say coach and player are 2 types of roles and I need to now allow coaches to add players and players to add themselves to coaches and have them related.
Is it possible to relate the User model role type coach and role type player in a separate table like coaches_players without creating new models for Player and Coach? If so can someone point me in the right direction. I know if I were to start from scratch I would have Player and Coach related through belongsToMany etc. but everything in my system works off of user_id and I do not want to change all the Auth functionality etc.
Is there a way to create those Player and Coach models that extend User and create the relationships through those?
Is there a way to relate 2 columns in the User table through a pivot table coaches_players?
Just looking for some guidance and possibly some links if you have them off the top of your head.
Sure, you can do this. Just create coaches_players table and define new relationships in User model:
public function players()
{
$this->belongsToMany(User::class, 'coaches_players', 'coach_id', 'player_id');
}
public function coaches()
{
$this->belongsToMany(User::class, 'coaches_players', 'player_id', 'coach_id');
}
I am following a tutorial on Laravel ACL, that takes into consideration Users and Roles (https://github.com/laracasts/laravel-5-roles-and-permissions-demo). However, I have an additional level of complexity whereby I need to take into consideration a company model also. So a user may belong to many companies and each instance of a company user may have many roles.
I think the best way to acheive this would use a pivot that has 3 fields:
company_id
role_id
user_id
Esentially, I want to achive something like this:
$user->companyRoles; //return the user's company roles
$user->company->assignRole('admin');
$user->assignRole($companyId, 'admin');
Can you advise on the relationships I require to do this?
Many thanks
I have a many-to-many relationship, users and roles, which I have worked out just fine. However, I have another table I would like to relate as well, called leads. A user and that user's role can be assigned to a lead.
So for example, I've created a Role and let's call it 'Manager'. Now, when I am managing my Users, I'd need to be able to assign different users to different roles which will be a many-to-many relationship (a role can have many users, a user can have many roles). So I will assign the role of 'Manager' to User A.
Now, when I am modifying my leads, I'd like to be able to assign a role_user to my lead (In my example, I'd like to assign a user to that lead), but I'd first need to assign a role to that Lead (Manager), and then be able to assign a user that is of that role to that lead.
Currently, I've got a many-to-many relationship setup for users and roles, using the pivot table name role_user. I've then got a many-to-many relationship setup on that pivot table, role_user, and leads, using another pivot table named lead_role_user.
Models + Controller - http://paste.laravel.com/D6h
My error: Call to undefined method Illuminate\Database\Query\Builder::roles()
It feels as though I am making this much more difficult than it should be.