Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

umairakram500's avatar

Laravel Query to Laravel Eloquent

Hi Everyone, Am new at Laravel, I want to get the list of Applications on the base of logged-in user. I have two tables LEVELS & APPLICATIONS. Both have two fields SEC_ID & LEVEL as well LEVELS table also contains USER_ID field. I want to get the list of APPLICATIONS if both table have same SEC_ID & LEVEL on the base of Logged-in USER_ID Which matched in LEVELS table.

1. levels
-id
-sec_id
-level
-user_id
2. applicants
-title
-details
-sec_id
-level
-status

Now i have get it with Query but i want to do with Models

DB::table('auth_levels')
            ->join('travel_apps', function($join){
                $join->on([
                    [ 'auth_levels.sec_id', '=', 'travel_apps.sec_id' ],
                    [ 'auth_levels.level', '=', 'travel_apps.level' ]
                ]);
            })
            ->where('auth_levels.user_id', '=', Auth::user()->id)
            ->select('travel_apps.*')
            ->get();

these are models i try to get.

class User extends Authenticatable
{
    public function levels()
    {
        return $this->hasMany(AuthLevel::class);
    }
}

class Level extends Model
{
    public function applications(){
        return $this->hasMany(TravelApp::class, 'sec_id', 'level');
    }

}

class Application extends Model
{
    public function levels(){
        return $this->belongsTo(AuthLevel::class, 'sec_id', 'level');
    }
}

$list = Auth:user()->levels->applications;

0 likes
1 reply
aurawindsurfing's avatar

Hi,

Class names do not correspond to each other:

AuthLevel::class is not class Level extends Modelshould be Level::class

TravelApp::class is not class Application extends Model should be Application::class

They must be the same.

Your tables could look a bit different if I'm getting it right:

1. users
-id
-name
-email
-title

2. applications
-id
-user_id
-level_id
-details
-status

3. levels
-id
-name

Hope it helps!

Please or to participate in this conversation.