如何获取基于ID的类别名称?

时间:2019-10-24 06:37:24

标签: php database laravel

我需要正确分配某些类别和主题的名称。如何获得基于ID的名称?

我有career_solutions表,其中有列topic_category_id(这是职业解决方案的类别ID)。我需要从表id的列category / categories中获得类别的名称。

现在,我只从career_solutions中获得{{ $user->topic_category_id }}的类别ID

这是我的控制人:

 $user = CareerSolution::where ( 'subject', 'LIKE', '%' . $q . '%' )
        ->join('role_users' , 'role_users.user_id', '=', 'career_solutions.user_id')
        ->join('roles' , 'roles.id', '=', 'role_users.role_id')
        ->join('users', 'users.id', '=', 'career_solutions.user_id')
        ->orWhere ( 'career_solutions.user_id', 'LIKE', '%' . $q . '%' )
        ->orWhere ( 'career_solutions.id', '=', 'events.subject')
        ->orWhere('career_solutions.topic_category_id' ,'=', $category->id)
        ->orWhere ( 'career_solutions.user_id', '=', 'users.username')
        ->select('career_solutions.id as id','subject','users.id as user_id','username', 'profile_picture', 'role_id', 'optional', 'topic_category_id')
         ->get ();


1 个答案:

答案 0 :(得分:1)

也加入类别表并从该表中获取类别名称

$user = CareerSolution::where ( 'subject', 'LIKE', '%' . $q . '%' )
        ->join('role_users' , 'role_users.user_id', '=', 'career_solutions.user_id')
        ->join('roles' , 'roles.id', '=', 'role_users.role_id')
        ->join('users', 'users.id', '=', 'career_solutions.user_id')
        ->join('categories', 'categories.id', '=', 'career_solutions.topic_category_id')
        ->orWhere ( 'career_solutions.user_id', 'LIKE', '%' . $q . '%' )
        ->orWhere ( 'career_solutions.id', '=', 'events.subject')
        ->orWhere('career_solutions.topic_category_id' ,'=', $category->id)
        ->orWhere ( 'career_solutions.user_id', '=', 'users.username')
        ->select('career_solutions.id as id','subject','users.id as user_id','username', 'profile_picture', 'role_id', 'optional', 'topic_category_id','categories.name')
         ->get ();

并致电{{ $user->name }}