Laravel - Find value is available in relationship - laravel

I need to check if the given user_id, location_id, and sub_location_id is belongs to the user on user_locations table. Based on this I need to give login permission to the user for the specific site.
Currently, I have the written code like below. But, don't know how to check the condition.
User::select('id')->with('user_locations:location')->where('user_id',$request->user_id)->get();
I need to put these conditions
is user found and active?
if user active. Then find the given sub location is tagged for the user.
if sub location found, then is the sub location active?
Below are the table structure.
Users Table
id (PK, AI),
user_id (UQ),
password,
status, ("Y"=> Active, "N"=> Inactive)
//other fields
public function user_locations(): HasMany
{
return $this->hasMany(user_locations::class);
}
User_Locations Table
id (PK, AI),
user_id (FK),
location_id (FK),
sub_location_id (FK),
status ("Y"=> Active, "N"=> Inactive)
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

I think you need to check relation existing
You can try this way for find user model
User::select('id')
->with('user_locations:location')
->where('user_id',$request->user_id)
->where('status','Y')
->whereHas('user_locations', function ($q) use ($request) {
$q->where('user_locations.user_id', $request->user_id);
$q->where('user_locations.status', 'Y');
})
->get();
If you want just to check user existing you can try this
User::where('user_id',$request->user_id)
->where('status','Y')
->whereHas('user_locations', function ($q) use ($request) {
$q->where('user_locations.user_id', $request->user_id);
$q->where('user_locations.status', 'Y');
})
->exists();
You can read mode about this here

Related

Eloquent Queries and Blade Display

I have my users table and a profile table with a user_id
in my User Model I have
public function profiles()
{
return $this->belongsTo('App\Models\Profile');
}
and the reverse in my Profile Model.
For my function to display on profile page I have
$profiles = Profile::get()
->where('users', 'user_id', '=', 'profile.user_id');
return view('profile.profile')->with('profiles', $profile');
but I am not sure this is correct. Trying to stay eloquent. I also don't understand how to display the info from users and profile table on my profile page. Can someone please help?
It may be as easy as this:
$profiles = $user->profiles;
return view('profile')->with('profiles', $profiles);
As long as you follow normal laravel naming conventions, which means that the users table is called users and has a primary key of id which is autoincrement. The profiles table is called profiles and has a FK called user_id.
If not, you may need:
public function profiles()
{
return $this->hasMany("App\Profile", "user_id");
}

Invalid argument supplied for foreach() in laravel when use Eloquent: Relationships

I wanna get the class name of a user via the user ID. When I input the ID of a user so I will wanna get the class name. I have three tables such as users table, classes table, and class_users table. The class_users table is born from two users table and classes table.
A users table has an id, name, email, password.
A classes table has an id, class_code, class_name.
A class_users table has an id, class_id, user_id
And this problem relates to Eloquent Relationships.
Thank you for help.
My Route:
Route::get('/find_classes/{id}','StudentController#find_classes');
My Controller function:
public function find_classes($id)
{
$users = User::find($id);
foreach($users->classes as $class)
{
echo $class->name . '<br';
dd($class);
}
}
My User Model:
public function classes()
{
return $this->belongsTo('App\Classes','class_users','user_id','class_id');
}
Looks like you might have the wrong relationship set up on your User model. You have a one to many set up, but your DB is setup to handle a many to many. I suggest you change your User model relationship:
public function classes()
{
return $this->belongsToMany('App\Classes');
}
Note, you may need to name the FK on that relation, as I see you have class_id on the table, but your actual class is named 'Classes'. Check through your relationships to ensure this is explicit on the FK where it doesn't follow Laravel convention exactly.
With this relationship, your foreach loop should work. It would be a good idea for efficiency, as mare96 noted, to eager load the classes on the $users collection when you query:
$users = User::with('classes')->find($id);

Laravel, How to retrieve parent records with certain Pivot table values belongsToMany

How can I retrieve all records of my model based on certain ID's in my pivot table?
I have the following 3 tables
users;
id,
name
stats;
id,
name
stats_selected;
user_id,
stats_id
Model
User.php
public function stats()
{
return $this->belongsToMany('App\stats', 'stats_selected', 'user_id', 'stats_id')->withTimestamps();
}
Controller
// Get all users with example of stats ID's
$aFilterWithStatsIDs = [1,10,13];
$oUser = User::with(['stats' => function ($query) use($aFilterWithStatsIDs ) {
$query->whereIn('stats_id', $aFilterWithStatsIDs);
}])
->orderBy('name', 'desc')
->get()
This outputs just all the users. Btw, fetching users with there stats and saving those selected stats into the DB is not a problem. That works fine with the above lines.
But how do I retrieve only the users which has certain stats_id's within them?
But how do I retrieve only the users which has certain stats_id's within them?
Use a whereHas conditional.
User::whereHas('stats', function ($stats) use ($aFilterWithStatsIDs) {
$stats->whereIn('id', $aFilterWithStatsIDs);
});

(Laravel / Eloquent) How to join 4 tables to make 1 query

I'm new at Laravel and I want some help with a Query. Here is my situation. I have 4 tables:
users (id, email, password.....)
user_profile (id, first_name, last_name,........, user_id)
companies (id, name,.......)
policies (id, policy_num, exp_date,........., user_id, company_id)
The table user have 5 users and every user have 10 policies. The table company have 3 companies.
$user_data=User::select('*')
->with('profile','policies')
->where('id', '=', $userId)
->first();
This code is working good, it retrieves the all the users and user_profile fields and all the policies(10) of this user with as you can see in this image, and I can show the user the details of the products on a home page.
I want to instead of appear the company_id from the policies table appears the name of the company from the company table.
Any help is appreciated.
Thank you for your answer ceejayoz, i already did.
User
public function profile()
{
return $this->hasOne('UserProfile');
}
public function policies()
{
return $this->hasMany('Policy');
}
Policy
public function company()
{
return $this->hasOne('Company');
}

How to get user's menu that has active modules in Laravel 4 with Eloquent?

I have tables to store user's menu like below:
Table: modules
id
name
status (will have values: active / inactive)
Table: menus
id
module_id
link
Table: menu_user
user_id
menu_id
join
Table: users
id
name
password
The relationship will be like:
[users]-<menu_user>-[menus]-[modules]
Modules has many Menus; and relationship between Users and Menus is many to many.
I only can get user's menu:
$data = User::with('menus')->where('id', 2);
Any how with Eloquent way that I want to have the users's menu where the modules has active status?
Another approach would be to come from the User end and use a nested relationship to the module, eg.
User::where('users.id', 1)
->with(array('menus.module' => function($query) {
$query->where('modules.status', '=', 'active');
})
)->get();
This is dependent on the following in the Models:
User
public function menus()
{
return $this->belongsToMany('Menu');
}
Menu
public function module(){
return $this->belongsTo('Module');
}
Module
public function menu(){
return $this->hasMany('Menu');
}
Glen

Resources