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;