User has many items with folders - php

I am trying to make an Eloquent query where I can get the Auths users expenses of a particual folder.
Example, get all Auth Users expenses that are linked to folder of id 2.
I have 3 tables,
expenses
-----------
id
folders
-----------
id
folder_expense
-----------
id
expense_id
folder_id
Currently I have these as my models.
User
---------------
public function expenses() {
return $this->hasMany('App\Expense', 'user_id', 'id');
}
public function folders() {
return $this->hasMany('App\Folder', 'user_id', 'id');
}
Expense
---------------
public function user() {
return $this->belongsTo('App/User', 'user_id');
}
public function folders() {
return $this->belongsToMany('App/Folder', 'folder_expense', 'folder_id', 'id');
}
Folder
---------------
public function user() {
return $this->belongsTo('App/User', 'user_id');
}
public function expenses() {
return $this->belongsToMany('App/Expense', 'folder_expense', 'expense_id', 'id');
}
I've made that pivot table thinking I could do something like this
Auth::user()->expenses()->folders()->where('folder_id', 2)->get()

How about?
Auth::user()->folders()->where('id', 2)->with(['expenses'])->get()

Related

Relationship between laravel models does not work

I have 2 models: one for users and one for clients. A user is a customer
User has a 'codigocli' field and client has a 'codigo' field
The relationships between my models are like this:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'codigo');
}
My database is fine (I think) client has the 'codigo' field and users has the 'codigocli' field. So what am I doing wrong? When I want to query my home.blade.php with dd(auth()->user()-cliente()) I don't get anything, although it shows me the parent object fine.
H
You have an OneToOne relationship here so try this if you don't change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli');
}
if you change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}

How can I make Laravel eloquent relationship with multiple tables?

I am trying to make 3 tables relationship like this,
users
id
name
roles
id
name
companies
id
name
company_role_user
user_id
role_id
company_id
Relationships in User.php model
public function companies() {
return $this->belongsToMany(Company::class, 'company_role_user', 'user_id', 'company_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'company_role_user', 'user_id', 'role_id');
}
public function role() {
// relationship to get role of specific company
}
Relationships in Company.php model
public function users() {
return $this->belongsToMany(User::class, 'company_role_user', 'company_id', 'user_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'company_role_user', 'company_id', 'role_id');
}
public function role() {
// relationship to get role of specific user
}
I want to get user role for specific company like this
User::find(1)->companies[0]->role->name
or
Company::find(1)->users[0]->role->name
If you have users
users
– id
– name
and you want them to have many to many relation with companies, you should do:
companies
– id
– name
user_companies
– user_id
– company_id
And for your roles, (many to many) you can do the same:
roles
- id
- name
user_roles
- user_id
- role_id
You are trying to make many to many 3 tables, when you should be doing it with 2 tables.
Even if you manage, it would be very complicated and confusing.
You should consider which tables should be related to roles, companies should have roles, or users should have roles, you dont need them both to have roles
Your schema looks correct but your model definitions/relations can use a junction/pivot model CompanyRoleUser which will relate these 3 relations as many-to-one/belongsTo and one-to-many/hasMany from Company/Role/User.
class Company extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'company_id');
}
}
class Role extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'role_id');
}
}
class User extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'user_id');
}
}
class CompanyRoleUser extends Model
{
public function company()
{
return $this->belongsTo(Discipline::class, 'id', 'company_id');
}
public function role()
{
return $this->belongsTo(Speciality::class, 'id', 'role_id');
}
public function user()
{
return $this->belongsTo(User::class ,'id','user_id');
}
}
Now you can apply different type of filters for your data like
Fetch users for company A id = 1 with admin role id = 2
User::whereHas('companyRoleUser.company', function ($query) use ($company_id) {
$query->where('id', $company_id);
})->whereHas('companyRoleUser.role', function ($query) use ($role_id) {
$query->where('id', $role_id);
});
Or
User::whereHas('companyRoleUser', function ($query) use ($company_id) {
$query->where('company_id', $company_id)
->where('role_id', $role_id);
});
Fetch companies with $user_id and $role_id
Company::whereHas('companyRoleUser', function ($query) use ($user_id) {
$query->where('user_id', $user_id)
->where('role_id', $role_id);
});

One to Many relation in laravel

I have 3 tables users companies company_users when I try to get user company details it return null
Logic
company has many users
user belong to 1 company
user model
public function company()
{
return $this->belongsTo(Company::class, 'company_users', 'user_id', 'company_id');
}
company model
public function user()
{
return $this->belongsToMany(User::class, 'company_users', 'company_id', 'user_id');
}
any idea?
it's not one to many relation it's many to many, with pivot table (company_users )
so, the relation should be like:
public function company()
{
return $this->belongsToMany(Company::class, 'company_users', 'user_id', 'company_id');
}
I recommend rename it to 'companies' cause it plural
Regarding to this comment:
If you really want one to many relation. Try this:
public function company()
{
return $this->hasMany(User::class);
}
public function user()
{
return $this->belongsTo(Company::class);
}

How to set relation in laravel with two ids?

I have these two tables:
product
- id
- name
favorites
- id
- product_id
- user_id
So, a user can add product to favorites only once. How can I set up this relation something like the following?
public function favorites() {
return $this->hasOne(Favorite::class, 'user_id', 'product_id')
}
So, I want to use both product_id & user_id such that the query would return proper result as per the following:
Get me the wishlist of user with id 1 and product with id 13!
you can do something like that:
in favourite Model class:
public function product(){
return $this->belongsTo('App\Product');
}
public function user(){
return $this->belongsTo('App\User');
}
In Product Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
In User Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
The user may have many favorites, so in Class User
public function favorites()
{
return $this->hasMany('App\Favorite');
}
Class Favorite
public function product()
{
return $this->belongsTo('App\Product');
}
public function user()
{
return $this->belongsTo('App\User');
}
If you have user you can
$userFavProducts = $user->favorites;
$product2 = $user->favorites()->where('product_id', 2)->get();
You should try this:
Favorites Model
public function product(){
return $this->belongsTo('App\Product','product_id');
}
public function user(){
return $this->belongsTo('App\User','user_id');
}

How to get records from 3 belongsToMany

I have a table structured like this.
user_company
user_id
company_id
user_dispensary
user_id
dispensary_id
user_group
user_id
group_id
// User.php
public function groups()
{
return $this->belongsToMany(Group::class,'user_group','user_id');
}
public function dispensary()
{
return $this->belongsToMany(Dispensary::class, 'user_dispensary', 'user_id');
}
public function company()
{
return $this->belongsToMany(Company::class, 'user_company', 'user_id');
}
// Company.php
public function dispensary()
{
return $this->hasMany(Dispensary::class);
}
public function users()
{
return $this->belongsToMany(User::class, 'user_company', 'company_id');
}
// Dispensary.php
public function company()
{
return $this->belongsTo(Company::class);
}
public function users()
{
return $this->belongsToMany(User::class, 'user_dispensary', 'dispensary_id');
}
I have tried this to get the users of the company where the user is.
\Auth::user()->company()->first()->users
But what I want is to get the records of all the users in a company where the current loggedin user is along with their groups and the users of the dispensaries where those dispensaries belongs to the company where the current user is.

Categories