I have three entities:
User
Profile
Status
Summarized, users has a profile foreach status.
EXAMPLE
Users table records
user
id | name
1 user1
2 user2
3 user3
Status table records (this records is previously seeded)
status
id (PK) | title (string)
1 student
2 collaborator
3 teacher
Profile table structure
PROFILE
user_status_id (PK) (FK)
institution (string)
year (timestamp)
user1 is registered as student and fill your profile based on this status. After, he change your status to collaborator and fill another profile referred to this new status.
With that, i want to know:
When user1 was student?
the user1 was already collaborator ever?
Which institution user1 had frequented when he was student?
My first problem is:
How to model this relationship?
I'm not sure but think the above situation is a ternary relatinoship case, right?
And Second problem:
How build using Laravel Eloquent Relationships?
Let's start with relationship
class User extends Eloquent{
protected $table = 'user';
public $timestamps = true;
use SoftDeletingTrait;
public function status(){
return $this->hasOne(Status::class, 'user_id', 'id');
}
public function profile(){
return $this->hasMany(Profile::class,'user_id','id');
}
}
class Status extends Eloquent{
protected $table = 'status';
public $timestamps = true;
use SoftDeletingTrait;
public function user(){
return $this->belongsTo(User::class,'user_id','id');
}
}
class Profile extends Eloquent{
protected $table = 'profile';
public $timestamps = true;
use SoftDeletingTrait;
public function user(){
return $this->belongsTo(User::class,'user_id','id');
}
}
Above relation would somehow will get you what you need but just writing this relation made me question your table structure.
Suggestion DB Change For Better Relation
User
id, username, email, ........
Status
id, status, description, ........
Institutions
id, name, address, ............
User_Status_Institution
user_id,status_id,institution_id,timestamp,.......
This structure allows us that we can have Institution details even if there is no user_profile for that institution. Previous structure you had you can only have institution (name only) detail if and only if there is user profile. Also you can change institution (Name/address/phone etc) at one place and it will update it for all users.
As much as possible try that you can make information to be independent if possible because that reflects in models, object and relations.
Hope it makes sense.
Related
I have the following tables:
Cards:
id (PK)
label
company_user_id (FK, Unique) (Foreign key on column Companies_Users.id )
Companies_Users:
id (PK)
company_id
user_uuid
Users:
uuid (PK)
name
Companies:
id (PK)
name
Every user can have one card per company. If a user is in multiple companies, he can have multiple cards.
Every card can be owned by one and only one user.
I need to set the relationship on the Card model that would get me the User owning the card.
What I did is:
class Card extends Model
{
use HasFactory;
protected $table = 'cards';
public $primaryKey = 'id';
public $incrementing = true;
public function user(){
return $this->hasOneThrough(User::class, CompanyUser::class, "user_uuid", "uuid", "company_user_id", "user_uuid");
}
}
But $card->user is always returning null.
How can I make it so I can fetch the user of a card without doing $card->companyUser->user. I need it to work directly using $card->user.
Thank you
Is it possible to have 2 additional user creation forms including the one that comes from Laravel.
1 form:
Users
User ID Name Email Password
2 form:
Employees
User ID Name Email Password and few more additional fields
3 form:
Residents
User ID Name Email Password and few more additional fields
I hope the above question makes sense, additional clarification: I'd like to join Users Table with employees and resident, just these 4 fields - UserID, name, email and password from Users Table the rest from Employees Table or Resident Table
Yes, you can but you need to specify the relationship in the models.
class Employee extends Model
{
protected $table = 'employees';
public function employee()
{
return $this->hasOne(User::class);
}
}
class Resident extends Model
{
protected $table = 'residents';
public function resident()
{
return $this->hasOne(User::class);
}
}
im trying to create a one to one relationship with the table user,student and teacher. The problem is, when i run LARAVEL TINKER, app\user::find('a123')->teacher it displays the b345 data and doesnt display NULL. New in Laravel. Thanks
Below is the table with their PK.
User table
user_id | type
a123 | 1
b345 | 2
Student table
student_id| name
a123 | Danny
Teacher table
teacher_id| name
b345 | Mr.Mark
and this BELOW is the models.
User Model
protected $primaryKey = 'user_id';
public function student(){
return $this->hasOne(student::class,'student_id');
}
public function teacher(){
return $this->hasOne(teacher::class,'teacher_id');
}
Student Model (Teacher model is just the same with teacher_id as PK)
protected $primaryKey = 'student_id';
public function User()
{
return $this->belongsTo(User::class,'user_id');
}
From you description, student should belongs to user, also teach
In User Model
public function student(){
return $this->hasMany(Student::class,'student_id');
}
In Student Model
public function user(){
return $this->belongsTo(User::class,'student_id');
}
The relevant portion of my application is set up as follows:
A Users table, with unique user IDs
A Teams table, with unique team IDs
A Team_Membership table, with
a unique ID and a column for a User ID and a Team ID, to denote
that a user belongs to a team.
A user can be on an unlimited number of teams.
I have a function to retrieve an array of the teams a given user belongs to. It would generally be called in the scope of the current logged in user to get their active teams, but will also be used in administrative functions to return the teams of a given user ID.
My first thought was to make the function callable from the user model, so for a given user object, I could do as follows
$teams = $user->get_teams();
or
$teams = User::get_teams($user_id);
But I'm not sure if this is generally considered best practice in Laravel for this type of functionality. Where should this function be located?
Actually, you are talking about something that laravel already does for you if you are using Eloquent.
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users';
public function teams()
{
return $this->belongsToMany(Team::class);
}
}
class Team extends Model
{
protected $table = 'teams';
public function users()
{
return $this->belongsToMany(User::class);
}
}
In addition to the users and teams table you would make a pivot table:
team_user
id - integer
team_id - integer
user_id - integer
and laravel does the rest.
$userId = 1;
$user = User::with('teams')->find($userId);
$teams = $user->teams;
This is what my schema looks like.
Schema:
User
id
email
password
firstname
lastname
Profile
user_id (foreign key)
name
weight
height
Model
I have a one to many relationship for the models.
User
class User extends Eloquent implements UserInterface, RemindableInterface {
public function Profiles()
{
return $this->hasMany('Profile');
}
Profile
class Profile extends Eloquent {
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
Problem
I realized that creating a separate name field in my Profile is already redundant to the firstname & lastname in User.
I plan to create a Profile property called name that is just referenced from the User model.
Any help on accomplishing this? Would appreciate any advice.
Thanks!