Laravel eloquent get specific user with his relations - php

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

Related

How to set value foreign key of foreign key Laravel

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

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

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

Laravel 5 - return only one field from relationship

have a relationship which returns rows from a pivot table.
However I just want to return a collection of the user ids from the table.
If I have it as current:
public function followers()
{
return $this->hasMany('App\Follower')->select(array('project_id', 'user_id'));
}
It will return the 2 fields, but if I just have the select with a single field:
public function followers()
{
return $this->hasMany('App\Follower')->select( 'user_id');
}
It returns an an empty object.
Any ideas?
I'm guessing this function is in your Project model. When modifying the select on a relationship, you must, at a minimum, select the fields needed to match the records together.
In this case, if this in on your Project model, if you don't select the project_id from the related table, Laravel can't match up the Follower objects with the correct Project objects, since Laravel won't have any idea which Project the Follower belongs to without the foreign key.

Laravel 4 eloquent pivot table

I have three tables: users, items and user_items. A user has many items and a item belongs to many users.
The tables:
Users
id
username
password
Items
id
name
equipable
User_items
id
user_id
item_id
equipped
The models:
class User extends Eloquent {
public function items()
{
return $this->belongsToMany('Item', 'user_items')
->withPivot('equipped');
}
}
class Item extends Eloquent {
public function users()
{
return $this->belongsToMany('User', 'user_items');
}
}
In the pivot (user_items) table I've a very important column named "equipped".
I've a form where users can equip, unequip and throw items. This form has a hidden field with the pivot (user_items) table row id. So, when a user tries to equip an item, the system checks if the item is equipable.
So, I want a object with the pivot data and the item data, based on item_id from the pivot table, I can send to the handler (where all logic is handled).
So what I've to do is to first access the pivot table and then access the item table.
Something like this (does not work):
$item = User::find(1)->items->pivot->find(1);
Is this possible to do?
You first have to include 'equipable' on your pivot call:
return $this->belongsToMany('User', 'user_items')
->withPivot('equipable');
Then you can ask Eloquent if your item is equipable or not:
$item = User::with('items')->get()->find(1)->items->find(2)->pivot->equipable;
Keep two things in mind:
Eloquent uses 'items' as a key internally, so that might interfere.
Whatever method you put before "get()" is part of the db query. Everything after "get()" is handled by PHP on the Object. The latter will be slower in most cases.

Categories