How to design user profiles in Laravel? - laravel

For several days I have been tring to design users profiles.
The system assumes four types users: Medical Personal, Patient, Operator, Admin.
Medical Personal can branch into three sub types: Dcotor, Manual Doctor, Massager.
Each user type has own profile data. For example Doctor has information about education and experience.
As result I need to get users profiles depends user type for authenticated user.
So my realization is to create a separate table for each user types with corresponsing models and relations to the user model:
class MedicalPersonal extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
class Patient extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
class Operator extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
And user model is:
public function medicalPersonal()
{
return $this->hasOne(MedicalPersonal::class);
}
public function patient()
{
return $this->hasOne(Patient::class);
}
I defined column user_type inside user migration to know the user type.
First problem I have faced is patient can be medical personal. But I have only one column. The second is how to be with sub types? Then can be added in the feature. It means I have to extent models by relations each time.
I tried to intoduce the model roles/permission. But in this case I can not bind profiles data to each roles. Because each profile has own set of columns.
How to solve it?

Related

Laravel eloquent one to many relationship data not showing

i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)
--
i have created two models.
student model:
class student extends Model
{
}
and
family model
class family extends Model
{
public function student()
{
return $this->hasMany('App\student');
}
}
--
i am able to show all data from student table,
my controller is:
public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }
--
what i tried
in family model:
return $this->hasMany('App\students');
what i want;
i want to connect family model with student model..
index page will have all students name only. which i have already done
when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.
In the student model:
class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}
}
Then you can access family information in Blade by:
{{$student->family->mobile_no}}
{{$student->family->profession}}
You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.
You should try this
class student extends Model
{
public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}
}
when you click on student name it will redirect to student/{id}
route
Route::get('student/{id}', 'StudentController#show');
student controller
public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}
student.show.blade file
Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}

How to create relationship in Laravel 5 which will collect records of own model mentioned in pivot table?

I have 3 tables users, companies and pivot table with user_id, company_id.
I can't get users, which belongs to my company inside User model.
Tried like
belongsToMany('App\User','companies_users','company_id','user_id' );
but I get relation with wrong users.
Since you are having a belongsToMany relationship between the User and Company, the User belongs to more than one Company. To get users of the companies of a particular User will not be straight forward. If you are sure that is exactly what you want, then do this:
//inside the User model
public function companies()
{
return $this->belongsToMany('Company');
}
//inside the User model
public function companiesusers()
{
$users= new Illuminate\Database\Eloquent\Collection;
foreach($this->companies as $company)
{
$users = $users->merge($company->users->get());
}
return $users->unique();
}
//inside the Company model
public function users()
{
return $this->belongsToMany('User');
}
Then you can get a user's companiesusers like so:
User::first()->companiesusers();

Laravel Relationship Union: One Relationship with two tables in a model

Lets supose this scenery:
We have 3 models -> User, Role and Permission.
Relationships
User has one to many relationship with Role.
Role has many to many relationship with Permission
User has many to many relationship with Permission.
The final purpose to the User -- Permisson relationships is to override the permission assigned to the user through his role. Just an exception for this User.
I am trying to to something like this:
public function permissions () {
return $this->hasManyThrough('App\Permission', 'App\Role')->merge($this->permissionOverrides);
}
In this case when I try to eager load the model permissions Laravel throws an error 'cause permissions is not detected as a relationships.
In the other hand, what if we change User -- Role relationship to many to many?
Thanks a lot for your knowledge!
EDIT
Role Relationships
public function permissions() {
return $this->belongsToMany('App\Permission')->withTimestamps();
}
Permission relationships
public function roles() {
return $this->belongsToMany('App\Role')->withTimestamps();
}
User relationships
public function roles() {
return $this->belongsToMany('App\Role')->withTimestamps();
}
public function permissionOverrides() {
return $this->belongsToMany('App\Permission')->withTimestamps();
}
The question is how to make the permissions() relationship merging the role permissions and the user permissions.
BETTER EXPLANATION
Due to misunderstanding shown in comments I will try to exlain this better. I had set all the relations between App\User and App\Role; I also had set the the relationship between App\Role and App\Permission. I can do right now:
$user->role->permissions;
For obtaining this I have configured the pivot table permission_role which stores the relation.
Thats not the case. What I want is to add one more var to the equation. Now I want to have the ability to override the role permissions adding a relationship between App\User and App\Permission. I have the pivot table permission_user and the relation stablished:
public function permissionsOverride() {
return $this->belongsToMany('App\Permission');
}
The REAL QUESTION is how to obtain all the permissions (both role permissions and overrided permissions in only ONE relationship). I mean merging both relationships.
Why I want to merge relationships? I could do a regular function to do this, but I would want to eager load this relation, Laravel way.
You can try like this, ManyToMany relation between User And Role, similarly ManyToMany relation between Role and Permission.
User Model
public function roles() {
return $this->belongsToMany('App\Role', 'user_role')->withTimestamps();
}
public function permissions() {
return $this->belongsToMany('App\Permission', 'permission_user')->withTimestamps();
}
Role Model
public function users() {
return $this->belongsToMany('App\User', 'user_role')->withTimestamps();
}
public function permissions() {
return $this->belongsToMany('App\Permission', 'permission_role')->withTimestamps();
}
Fetch Data
$user = User::with('permissions','roles','roles.permissions')->find(1);
$permissions = $user->permissions;
foreach($user->roles as $role){
$permissions->merge($role->permissions); //merger collection
}
dd($permissions->all());
For details you can look https://laravel.com/docs/5.6/eloquent-relationships#many-to-many
And for Collection you can look this https://laravel.com/docs/5.6/collections#method-merge

User to User Model: One to Many Relationship on Laravel

I'm doing a project for one of my University courses. But I'm finding it difficult to make relationship between User to User model.
Table: users
id
name
email
type
password
On this table the column type is used for classifying user types.
For example: User can be either distributor or dealer type.
And a distributor may have many dealer
But a dealer may have only one distributor.
To implement this I've created another table and named as dealers_of_distributor.
Table: dealers_of_distributor
id
distributor_id (id of users table)
dealer_id (id of users table)
Although I've used an additional model DelaersOfDistributor, the exact relationship should be between user to user.
How can I make this relationship. I'm confused!
I've tried so far:
Model: User
public function dealersOfDistributor(){
$this->hasMany('App\DealersOfDistributor');
}
Model: DealersOfDistributor
public function user(){
return $this->belongsTo('App\User');
}
The DistributorsController
public function index()
{
$distributors=User::where('type', 'distributor')->get();
$dealers=User::where('type', 'dealer')->get();
$assignedDealers=DealersOfDistributor::all();
return view('distributors.index')->withDistributors($distributors)->withDealers($dealers)->with('assignedDealers', $assignedDealers);
}
In blade file: I've tried $assignedDealer->dealer_id->user->name. But It's showing error.
Model DealersOfDistributor:
public function distributorUser()
{
return $this->belongsTo('App\User', 'distributor_id', 'id');
}
public function dealerUser()
{
return $this->belongsTo('App\User', 'dealer_id', 'id');
}
than you just get the user's data from DealersOfDistributor model, call function dealerUser if you want get user's data from dealer_id, and call function distributionUser if you want get user's data from distribution_id
Controller:
public function index()
{
$assignedDealers=DealersOfDistributor::all();
return view('distributors.index',compact('assignedDealers'));
}
Blade get username:
#foreach($assignedDealers as $assign)
{{$assign->distributorUser->name}}
{{$assign->dealerUser->name}}
#endforeach

Laravel - two columns with relationship to the same column in another table

I set up a one-to-many Eloquent relationship between User and Credit
User
id | name
Credit
id | recipient | author | amount
where
recipient is user.id
author is user.id
In Laravel, the relationship for recipient is established:
class Credit extends Model {
public function user() {
return $this->belongsTo('App\User', 'recipient');
}
}
But how do I add the relationship for author as well?
I tried:
class Credit extends Model {
public function recipient() {
return $this->belongsTo('App\User', 'recipient');
}
public function author() {
return $this->belongsTo('App\User', 'author');
}
}
i.e. I changed user() into two functions: recipient() and author()
But I am getting Trying to get property of non-object, which I think means that using recipient() or author(), Eloquent is unable to "bind" it to my User model.
How do I get around this?
The problem is that you have a field called recipient and a relationship sharing the same name. Consider renaming one of them this is because laravel will use the field instead hence if you do
$credit->recipient->name
you can't access the user table since laravel will load the field. And the field does not have a property called name.
Earlier when the relationship was user() it worked. So consider finding a suitable name to user e.g credit_recipient()

Resources