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
Related
I have a relationship between two models, User and Follower.
User hasMany Follower, and Follower belongsTo User.
In my followers table in the database I have two columns, follower_id and following_id. Both these columns refer to the id column in the users table.
My follower and users table
followers
id
follower_id
following_id
users
id
name
username
User Model
public function followers(){
return $this->hasMany('App\Follower', 'follower_id');
}
public function followings(){
return $this->hasMany('App\Follower', 'following_id');
}
Follower Model
public function user(){
return $this->belongsTo('App\User');
}
In a test controller I am doing this:
$followed = Follower::find(2);
From here, I would like to select one of the columns, follower_id or following_id, and access the user that this particular row belongs to by using $followed->user->name. How would I go about that? Since I need to select a column before accessing, I am a little bit confused.
What can I do to access the data I need?
Your table should be :
followers
id
follower_id
following_id
user_id
users
id
name
username
In your Follower model :
public function user()
{
return $this->hasMany(Follower::class, 'user_id');
}
In your User model :
public function follower()
{
return $this->belongsTo(User::class, 'user_id');
}
Then you can query like this :
$follower = Follower::with('user')->findorFail(2);
$user_name = $follower->user->name;
So im trying to figure out how to use pivot tables in Laravel. And I have tried to read and understand the documentation. And I cannot understand why a user just can't balongTo a "department". I don't want a user to belongToMany "departments".
Departments model
public function users()
{
return $this->belongsToMany(User::class, 'department_user', 'user_id', 'department_id');
}
And for the user model, I want something like
public function department()
{
return $this->belongsTo(Department::class);
}
But Laravel reports null. Because department_id doesn't exist in the users row? How can I reverse this pivot table lookup, with belongsToMany and belongsTo?
I think you're getting confused with your relationship types. If I understand you correctly:
A Department hasMany Users.
A User belongsTo a Department.
This one-to-many relationship type does not need a pivot table and can be written as such:
class Department extend Eloquent
{
public function users()
{
return $this->hasMany(User::class);
}
}
class User extend Eloquent
{
public function department()
{
return $this->belongsTo(Department::class);
}
}
This would require that the users table have a department_id foreign key.
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
In my app I have Users and Tickets.
In the table 'tickets' I have one foreign key, like this:
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
In the model 'Ticket' I have this relationship:
public function owner()
{
return $this->belongsTo('App\User');
}
User model:
public function createdTickets()
{
return $this->hasMany('App\Ticket');
}
By doing this:
dd(\Auth::user()->createdTickets()->get()->toArray());
I get all tickets, no problem here. But when I try to get the owner from the Ticket, it gives me null:
$ticket = \Auth::user()->createdTickets()->first();
dd($ticket->owner); //if I try dd($ticket) it juts fine.
PS: For the record, I have another type of relationship between both model (many to many)
Ticket:
public function followers()
{
return $this->belongsToMany('App\User')->withTimeStamps();
}
User:
public function followingTickets()
{
return $this->belongsToMany('App\Ticket')->withTimeStamps();
}
I got my answer on laracasts forums:
Laracasts
for "belongsTo" relation eloquent uses the method name on the foreign key. I am using owner(), so eloquent was looking for owner_id field.
So I had to do this:
public function owner()
{
return $this->belongsTo('App\User', 'user_id');
}
I'm trying to implement a friendlist for a user
so a user can have many friends
but for calling the friends I'm having trouble getting my relation right:
user
id
friends
user_id
friend_user_id
Class user:
public function Friends(){
return $this->hasMany('App\User', 'friends');
}
So Auth::User()->Friends should return a list of User models
You actually don't have a "one to many" relationship but rather "many to many" with friends being your joining table
Try this:
public function Friends(){
return $this->belongsToMany('App\User', 'friends', 'user_id', 'friend_user_id');
}
When creating a relation to the same model, you would do something like this:
Models/User.cs
public function Friends()
{
return $this->hasMany('User', 'user_friend_of', 'id'); // assuming user_friend_of is the FK on user-table
}
public function User()
{
return $this->belongsTo('User');
}
This would mean you have only 1 table, which has a foreign key to another row in the same table, for instance: (id - name - user_friend_of )
In your user class, define the function as follows:
public function Friends(){
return $this->hasMany('friends');
}
Calling $user->Friends()->get() should return you everything you need. Note that 'friends' should be the name of the table.