Php 如何根据id获取类别名称?

Php 如何根据id获取类别名称?,php,database,laravel,Php,Database,Laravel,我需要正确分配一些类别和主题的名称。如何根据id获取名称 我有career\u solutions表,表中有topic\u category\u id(这是career solutions的category id)。我需要从表categories中的id/category列中获取类别名称 现在,我只从career\u solutions和{{{$user->topic\u category\u id} 这是我的控制器: $user = CareerSolution::where ( 'subj

我需要正确分配一些类别和主题的名称。如何根据id获取名称

我有
career\u solutions
表,表中有
topic\u category\u id
(这是career solutions的category id)。我需要从表
categories
中的
id
/
category
列中获取类别名称

现在,我只从
career\u solutions
{{{$user->topic\u category\u 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 ();



也连接category表并从该表中获取categoryname

$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}