skip to Main Content

How can I write query for below table in Laravel.
Like In my app logged user id is 109
So I need whose user match with logged user in BlindDate and PaidDate table to user id.

This is User table

This is BlindDate table

THis is PaidDate table

I want need expected output is :

data[
  
  'blind_users':[
     {
        'uuid':'rvfv',
        'user_id':109,
        'touser':[
            {
              'name':'james',
              'email':'[email protected]'
            }  
         ]

     },
     {
        'uuid':'rvfv',
        'user_id':109,
        'touser':[
            {
              'name':'james',
              'email':'[email protected]'
            }  
         ]

     },
  ],

 'paid_date_users':[
   {
        'uuid':'rvfv',
        'user_id':109,
        'touser':[
            {
              'name':'james',
              'email':'[email protected]'
            }  
         ]

     },
  ]

]

2

Answers


  1. Hopefully you have setup the eloquent Model and its relations since you are using Laravel for this project!

    If you only have the user id you can query the User model like so

    return User::with('blind_date', 'paid_date')->where('id', $userId)->first();
    

    If you have an authenticated user you can just return the user without any eager loading or just call it in you blade like auth()->user()

    Login or Signup to reply.
  2. Ans: Use these two function in user model.

    public function bind_dates(){
      return $this->belongsTo('binddates','user_id');
    }
        
    public function free_paid_dates(){
      return $this->belongsTo('freepaiddates','user_id');
    }
    

    Query:

    Users::with('free_paid_dates','bind_dates')->where('id',$user_id)->get();
    

    If you have a multiple user, user hasMany relation.

    Please use this code, I think your code working.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search