laravel-relations

Laravel API JSON customization and table relationship

允我心安 提交于 2021-01-29 08:09:08
问题 I'm developing a social media project using Laravel. I need to provide a customized API for Follows and Followers. Accordingly, I want to establish a relationship between the user table and the followers and followed tables. I have looked at a lot of resources but get confused. Can anyone help me with relationships? Models Users: id, username, email, password Follows: id, user_id, follow_id(user_id) Followers: id, user_id, follower_id(user_id) API Url: http://localhost/api/follows/user_id/1

Laravel Many to Many Sync with additional column

喜你入骨 提交于 2020-08-07 15:58:35
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

南楼画角 提交于 2020-08-07 15:58:10
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

穿精又带淫゛_ 提交于 2020-08-07 15:51:26
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

核能气质少年 提交于 2020-08-07 15:51:05
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.