0

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

  1. is user found and active?
  2. if user active. Then find the given sub location is tagged for the user.
  3. 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);
}
4
  • You need to get the model of user with user_locations or just check if there is user with these conditions or not? Commented Aug 10, 2022 at 12:12
  • I just need to check if there is a user with the corresponding location or not, to allow the user to move forward to the login process. Commented Aug 10, 2022 at 12:14
  • And how many levels can be in locations? I mean each location has one sub_location or sub_location can be its own sub_location too? Commented Aug 10, 2022 at 12:18
  • Each location has many sub locations. Commented Aug 10, 2022 at 12:36

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

$q->where('user_locations.user_id', $request->user_id); is not working because in user_locations table we have mapped only user's auto increment id (int). But, we are getting the user_id (string) from the user.
I have tried like this but it's not working. $user = User::where('user_id',$request->user_id) ->where('status','Y') ->whereHas('user_locations', function ($q) use ($request) { $q->where('user_locations.user_id', 'users.id'); $q->where('user_locations.location_master', $request->location); $q->where('user_locations.status', 'Y'); })->exists();
I just removed $q->where('user_locations.user_id', $request->user_id); and it's working fine. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.