User to User Model: One to Many Relationship on Laravel - 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

Related

How to design user profiles in 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?

Accessing 3 tables in Laravel using foreign key (laravel 6)

I want to show data from a database(sql) that is from a specific user.
I have 3 tables:
Users
Stores
Areas
These are the following relationship between tables:
User hasOne Area
Area hasMany Stores
Basically what I wanted to show is that every user has their own area that has many stores.
User model
function area() {
return $this->hasOne('App\Area');
}
Area model
function user() {
return $this->belongsTo('App\User');
}
function stores() {
return $this->hasMany('App\Store');
}
Store Model
function area() {
return $this->belongsTo('App\Area');
}
My database looks like this:
user table
id name role_id area_id
area table
id name user_id
store table
id name area_id
How can I access user->area->store?
This is what I got so far
function show() {
$id= Auth::user()->id;
$user = User::find($id);
echo($user->area->stores);
}
Thank you
you can use the hasManyThrough for get sotres of an area:
define stores function in User model
public function stores()
{
return $this->hasManyThrough(Store::class, Area::class);
}
and use it :
$stores = auth()->user()->stores;
I hope be useful.

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();

Eloquent Relationship with Laravel 5.4

I have the following Models with corresponding tables:
User (users), UserProfile (user_profiles), Review (reviews)
Schema
table: users
user_id (primary key)
first_name
last_name
table: user_profiles
user_id (foreign key)
country
phone
table: reviews
reviewer_id (the user who posted the review)
user_id (foreign key)
body
I query the database with the following:
$user = User::with('profile', 'review')->find(Auth::User()->user_id);
Part of User Model
public function profile(){
return $this->hasOne(UserProfile::class, 'user_id');
}
public function review(){
return $this->hasOne(Review::class, 'user_id');
}
Part of UserProfile Model
public function users() {
return $this->belongsTo(User::class, 'user_id');
}
Part of Review Model
public function users() {
return $this->belongsTo(User::class, 'user_id');
}
How can I access the details of the reviewer using the reviewer_id from the users table
As a sunup of what was discussed in this question, I think the major concern of OP was regarding a table that have 2 foreign keys with the same table. Both user_id and reviewer_id are foreign keys linked to the users table.
One important thing to understand about Laravel relationships is that the relationship name is not hard bound, it can be anything as long as it makes sense to you. The related object will be available in the ATTRIBUTE with the exact name of the METHOD.
class User extends Model {
public function review() {
return $this->hasOne(Review::class);
}
}
A user's review will be available at User::find(1)->review;.
class Review extends Model {
public function owner() {
return $this->belongsTo(User::class, 'user_id');
}
public function reviewer() {
return $this->belongsTo(User::class, 'reviewer_id');
}
}
From a review, you will be able to access both users:
Review::find(1)->owner; // This is the relationship with user_id column
Review::find(1)->reviewer; // This is the relationship with reviewer_id column

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