How to set value foreign key of foreign key Laravel - php

Visit Model
id
user_id
visit_date
public function user()
{
return $this->belongsTo(USER::class,'user_id','user_id')->withTrashed();
}
User Model
user_id
branch_id
name
public function branch()
{
return $this->belongsTo(BRANCH::class,'branch_id','branch_id')->withTrashed();
}
Branch Model
branch_id
branch_desc
I want to display branch_desc when getting list of Visit.
Currently Im showing only branch_id from User Model

You need to make one to Many relation from visit to user table and user to branch table
ex. of relationship code:
//From Visit to User model
$this->belongsTo(User::class, 'user_id')
//From User to Branch Model
$this-belongsTo(Branch::clas, 'branch_id')
This will allow you to eager load the relationships in you controller like this
Visit::with(['user.branch'])
//Now you will get all visits with neseted users and branch
I will recommend you to take a look at laravel relationships docs for a better understanding
Laravel Relationships with eager loading

For 2 layers (or more) relationships, I recommend you install this package from Staudenmeir https://github.com/staudenmeir/eloquent-has-many-deep
Inside your Visit model, you can add the relationship like this:
public function branches() {
$query-> hasManyThrough(Branch::class, User::class);
}
And for more information , check LaravelDaily tutorial related to this package https://youtu.be/wgdWokrm3Mw

Your question isn't clear really! However from what I understand from your question you should also have a Branch model and migration.
So, from visit table you will fetch the users by the relation with user in your visit model.
public function user()
{
return $this->belongsTo(User::class, 'id', 'user_id');
}
Then when you can fetch your user branches from your User model by your users relation with Branch.
public function branch()
{
return $this->hasMany(Branch::class, 'user_id', 'id');
}
So, you will fetch data like this:
$visit->user->branch->description
You can replace description with any column_name_that_wish_to_fetch.

If figured it out.
Use this in getting list of your model in Controller.
This is how to query with multiple relationship
$visit= VISIT::with('USER.BRANCH')->orderBy('user_id','desc');

Related

How to get a particular column value by using Eloquent ORM?

I have two migration tables and two models
1)User.
2)Subscription.
in subscriptions Model i write one method called user() which has hasMany relationship and in Users model it will contain subscriptions() method which have belongs() relationship.
in user table id acts as an primary key and in subscription table sub_id acts as a foriegn key,for example if the user subscribed to any subscription the user id will store in the sub_id in the subscription table upto this it's working fine.
$is_subscription=Subscription::where('sub_id',$user->id)->value('sub_id');
from the above code i am able to get the value but now what i want is is there any better way to write the query based on models ,please help me to write this one..
According to your descriptions, I believe this is what you have:
class User extends Model
{
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
}
class Subscription extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
So you want to check whether an user has any subscriptions. You can do one of the followings:
$user->subscriptions()->exists();
// Or
$user->subscriptions->isNotEmpty();
You can use pluck() method:
$is_subscription=Subscription::where('sub_id',$user->id)->pluck('sub_id');
You can find details on the methods that are available for collection using the Laravel Documentation

The relationship of one model

I have a problem with Laravel relationships.
I need to make relationship based on this table:
issuer and friend need to be united. Relationship will return all rows where user id in issuer or in friend. At the moment code looks like this:
return DB::table('contacts')->select()->where('friend', $this->id)->orWhere('issuer', $this->id)->where('status', 'approved');
Previously I used that method, but there are no relationship, 'cause attach() is undefined.
private function contactsIssued() {
return $this->belongsToMany(User::class, 'contacts','issuer', 'friend');
}
private function contactsFriended() {
return $this->belongsToMany(User::class, 'contacts','friend', 'issuer');
}
public function contacts() {
return $this->contactsIssued()->union($this->contactsFriended()->toBase());
}
So, I need to make one relationship that has two foreign columns.
Sorry, my English can be broken, 'cause it's not my native language.
That's look like a one to many relationship but you are using belongsToMany which is many to many relationship and required a pivot table.
if you have user model that has many contact model then in your contact model you should use the one to many relationship by using belongsTo.
but if you have 3 table you didn't say that in you question then please edit you question and provide your models and the relations between them.

Modelling the relationship between User and Rank models

I am using Laravel & Eloquent.
I have two tables.
User
id, username, password, rankId
Rank
id, rankName
What would be the relationship between both of these? And, how would I possible show the relationship between both of these models using Laravel Eloquent Models? Any help would be highly appreciated.
I thought of hasOne, but then, it requires the second table (rank) to have a foreign key to the users table. How would I go about defining this relationship in Laravel?
If you are on User model,that will belongs to relation,like
public function rank()
{
return return $this->belongsTo('App\Rank', 'rankId', 'id');
}
Now if you query from User model,you can get rank information like this,
$user = User::find(1);
var_dump($user->rank->rankName);
Now if you are on Rank model and you want to get all user in a specific rank,so your relation will be has many,like this
public function users()
{
return $this->hasMany('App\User', 'rankId', 'id');
}
Now if you query a rank,you can get user by this way
$ranks = Rank::find(1);
foreach($ranks->users as $user){
var_dump($user->username);
}
May be everything is clear to you now

How to establish a hasManyThrough relationship in laravel for roles and permissions?

So I understand how to load the roles for a user and the permissions for a role.
But now I have a user table, a role table, and a permission table. I also have role_user table for linking users and roles. And of course a permission_role for linking permissions and roles.
Now when I want to get all the roles for the user, I simply do something like this:
public function roles()
{
return $this->belongsToMany('Uppdragshuset\AO\Tenant\Models\Role');
}
Similarly I can fetch permissions for roles like so:
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
Now according to the documentation on laravel, I can directly fetch permissions for a user by using the hasManyThrough relation too like so:
public function permissions()
{
return $this->hasManyThrough(Permission::class, Role::class);
}
But this is returning with an error saying:
Unknown column 'role.user_id' in 'field list'
I think I understand why. Laravel is looking for user_id field in the role table but it does not understand that it is a many to many relation and it should look for it in the pivot table.
So what is the way around this? Is there a way around this in Eloquent or will I have to resort to using the query builder? And if yes, how to do the same thing with the query builder?
HasManyThrough can only be used to connect two HasMany relationships. There is no native relationship for your case.
I created a HasManyThrough relationship with unlimited levels and support for BelongsToMany:
Repository on GitHub
After the installation, you can use it like this:
class User extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions() {
return $this->hasManyDeep(Permission::class, ['role_user', Role::class, 'permission_role']);
}
}

Laravel eloquent get specific user with his relations

Hello i am trying to get one user with his relationships. So one user form user table could have many items, so relationship is one to many.
Booth model
public function booths() {
return $this->hasMany('App\Booth');
}
User model
public function users() {
return $this->belongsTo('App\User');
}
I set foreign key from Booths table user_id is connected with Id from User table.
I try this query
$booth =User::findOrFail(83)->booths()->get();
But get all users. Also tried with with() but in that case i get all booths.
Change your code to
$user = User::with('booths')->findOrFail(83);

Categories