반응형
Laravel join 쿼리 AS
어떤 방식으로든 다음을 정의합니다.AS
질문을 위해 ?
저는 다음을 시도해 보았습니다.
$data = News::order_by('news.id', 'desc')
->join('categories', 'news.category_id', '=', 'categories.id')
->left_join('users', 'news.user_id', '=', 'users.id') // ['created_by']
->left_join('users', 'news.modified_by', '=', 'users.id') // ['modified_by']
->paginate(30, array('news.title', 'categories.name as categories', 'users.name as username'));
문제는.['name']
카테고리에서 다른 것으로 대체될 것입니다.users
. 다른 이름을 가진 그들을 가질 방법은 없습니까?
위의 별칭을 사용하는 중...둘 다 참여하는 별칭을 만들려면 어떻게 해야 합니까?users.name
?
paginate()
메서드의 두 번째 매개 변수는 쿼리에서 선택할 테이블 열 배열을 허용합니다.그래서 이 부분은
paginate(30, array('news.title, category.name'));
다음과 같음:
paginate(30, array('news.title', 'category.name'));
갱신하다 (질문을 변경한 후)
시도해 보기:
->paginate(30, array('news.title', 'categories.name as category_name', 'users.name as user_name'));
업데이트 2 (질문을 변경한 후 다시)
테이블에서도 별칭을 사용할 수 있습니다.
$data = News::order_by('news.id', 'desc')
->join('categories', 'news.category_id', '=', 'categories.id')
->join('users as u1', 'news.user_id', '=', 'u1.id') // ['created_by']
->join('users as u2', 'news.modified_by', '=', 'u2.id') // ['modified_by']
->paginate(30, array('news.title', 'categories.name as categories', 'u1.name as creater_username', 'u2.name as modifier_username'));
이 질문에 대한 보다 단순하고 간단한 답은 다음과 같습니다. 그리고 제가 찾고 있던 것은 다음과 같은 테이블 이름이나 열로 별칭을 직접 지원한다는 것입니다.
$users = DB::table('really_long_table_name AS t')
->select('t.id AS uid')
->get();
언급URL : https://stackoverflow.com/questions/14318205/laravel-join-queries-as
반응형
'itsource' 카테고리의 다른 글
MySQL Workbench 테이블 데이터 가져오기 마법사 매우 느림 (0) | 2023.09.19 |
---|---|
워드프레스에서 페이지 새로 고침 없이 연락처 양식 7을 제출하는 방법은? (0) | 2023.09.19 |
Angular Application에 여러 HTTP 인터셉터 추가 (0) | 2023.09.14 |
"if" 문을 사용하여 마지막 명령의 종료 코드 확인 (0) | 2023.09.14 |
Mariadb 열 저장소에서 텍스트 데이터 형식을 만들 수 없습니다. (0) | 2023.09.14 |