我需要正确分配某些类别和主题的名称。如何获得基于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 ();
答案 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 }}