I want to put all my different users (teachers, students, admins) in the same table but how can I differentiate between them.
When I add the relationships in this table, for example : the teachers belongs to a departement table:
public function departement()
{
return $this->belongsTo(Departement::class);
}
And the students belongs to another table which is faculty table:
public function faculty()
{
return $this->belongsTo(Faculty::class);
}
The problem is when I add those relationships to the users table it will add automatically to all types of users (teachers, students, admins...).
use where
public function departement()
{
return $this->belongsTo(Departement::class)->where('users.type','=','student');
}
public function faculty()
{
return $this->belongsTo(Faculty::class)->where('users.type','=','admin');
}
Related
I have the following table structure:
default_tasks
id
...
locations
id
...
users
id
...
default_task_location
id
location_id
default_task_id
default_assignee_id (FK to users.id)
In my blade file I'm looping through the default tasks for the location, and trying to output the default assigned user. I can get the ID by doing $task->pivot->default_assignee_id, but I want to get the actual user model so I can output the user's name in my blade file
I've gathered from searching that the way to accomplish this is to create a pivot table model. So I've done this:
class DefaultTaskLocationPivot extends Pivot
{
public function user()
{
return $this->belongsTo(User::class, 'default_assignee_id');
}
public function defaultTask()
{
return $this->belongsTo(DefaultTask::class);
}
public function location()
{
return $this->belongsTo(Location::class);
}
}
and in my location model I've done this:
public function taskDefaultAssignee()
{
return $this->belongsToMany(User::class)
->using(DefaultTaskLocationPivot::class)->withPivot(['default_task_id', 'frequency_type_override', 'create_on_day_override', 'due_on_day_override', 'default_assignee_id']);
}
However, I'm still unable to access the user associated with the default_assignee_id. I've tried things like $task->location->taskDefaultAssignee, but it doesn't seem like the relationship is even available if I dump the object.
i am unable to find best suited relations for creating a courier website.
my requirements are
a branch can have many users
a franchise can be associated with only one user
a branch can have many franchise
branch user can access through franchise models
Note:- branch and franchise have different profiles
I want to avoid directly jumping to the answer and convert the statements you have given into more easier relationship descriptions :
- 1 branch has many franchises
- 1 user has 1 franchise
This will then help you to understand :
- branch <> franchises is one to many relationship
- franchise <> user is one to one relationship
Models :
Branch Model :
public function franchises()
{
return $this->hasMany(Franchise::class);
}
Franchise Model :
public function branch()
{
return $this->belongsTo(Branch::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
User Model :
public function franchise()
{
return $this->hasOne(User::class);
}
To make these relationships work, you need to have proper foreign keys in your database schema. To know more please check documentation
I have 4 tables,
props, listing, offers, contact
props has many listing, listing belongs to props
public function listings()
{
return $this->hasMany('App\Models\Listing\Listing');
}
offer belongs to listing,
public function property()
{
return $this->belongsTo('App\Models\Property\Property')->with('owners');
}
then
offer belongsToMany contact trough offer_contact table
public function buyers()
{
return $this->belongsToMany(Contact::class, 'offer_contact', 'offer_id', 'contact_id')->with('primary_email');
}
My question is, how to access buyers()?
Something like $props->buyers()
In props model, what I did is
return $this->hasManyThrough('App\Models\Offer\Offer', 'App\Models\Listing\Listing');
You cannot. You may use nested iterations to get properties, listings belongs to each property, offers belongs to each listing and then customers belonging with the offer.
Alternatively, you may use the raw query to get the desired result using DB::statement();
I created a HasManyThrough relationship with unlimited levels: Repository on GitHub
After the installation, you can use it like this:
class Property extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function buyers() {
return $this->hasManyDeep(Contact::class, [Listing::class, Offer::class, 'offer_contact']);
}
}
I have a user_managers pivot table that gets both keys from the users table:
employer_user_id
employee_user_id
I believe users would have a many to many relationship as a user can be managed by more than 1 manager there will be users who manage 1 or more users, while other users (excluding those under them) would manage them while there will be users who don't manage at all and a User can have only 1 manager.
My first try at defining this was to build another model named Manager representing the user_managers pivot table, so in User model I wrote the following 2 functions:
public function managedBy()
{
return $this->belongsToMany('Manager', 'employer_user_id');
}
public function manages()
{
return $this->hasMany('Manager', 'employee_user_id', 'employer_user_id');
}
Does this make sense or do you know of a better way to implement this kind of structure?
If a user can have only 1 manager then you can define you relationship as one to many like
//User model
public function managedBy()
{
return $this->belongsTo(User::class, 'manager_id');
}
public function managees()
{
return $this->hasMany(User::class, 'manager_id');
}
you don't need to pass $id to your relationship definition.
For multiple managers, Yes you would need a many to many relationship by adding a junction/pivot table which i guess you already have user_managers, Now you need to define your relationships using belongsToMany for managers and mangees like
public function managers()
{
$this->belongsToMany(User::class, 'user_managers', 'employer_user_id')
}
public function managees()
{
$this->belongsToMany(User::class, 'user_managers', 'employee_user_id')
}
I have a quick question on how to define the below relationship
I have a USER that can belong to Many Councils, Many Schools and Many Businesses.
Now I know I can have a pivot table for all the above something like
council_user
school_user
business_user
This means I can pull out of the DB all councils a user belongs to, all businesses etc
To save me doing this is there a better approach that having 3 pivot tables, could I use Many To Many Polymorphic Relations and do it that way, if so does any one know how that would look as a table structure?
I feel it would be something like...
business
id - integer
name - string
council
id - integer
name - string
school
id - integer
name - string
userables
user_id - integer
userable_id - integer
userable_type - string
If so do I remove the userable_is and userable_type from my USER table and have this extra userables table?
Does any one know how this works, have I totally misunderstood what Polymorphic Relations does?
In Many to Many polymorphic relationship, you should have userables table along with users table. and userable_id and userable_type should be in userables table not in users table.
userables table:
user_id userable_id userable_type
1 1 App\Business
1 2 App\School
...
Business, School and Council Model:
public function users()
{
return $this->morphToMany('App\User', 'userable');
}
User Model:
public function businesses()
{
return $this->morphedByMany('App\User', 'userable');
}
public function schools()
{
return $this->morphedByMany('App\User', 'userable');
}
public function councils()
{
return $this->morphedByMany('App\User', 'userable');
}
then, this gives all the businesses of user with user_id 1.
$users=User::find(1);
foreach($user->businesses as $business)
{
print_r($business->name);
}
See Many to many polymorphic relation in doc.