엘로퀀트에서 one to one join 연결의 controller에서 select 해오는 방법이 있을까요?예를 들어서 제가 만들고 싶은 건
select a.*, b.log_time from a_table as a inner join b_table as b on a.id = b.id이런 식의 쿼리를 구현하고 싶습니다.
그래서,
Atable 모델은
public function log_time(){ return $this→hasOne(Btable::class, 'fk_id', 'id');}
Atable::where('reg_time','>','now()')→with('log_time') 이런식으로 했는데,toSql로 찍어보니 join 과 관련된 쿼리는 없더라구요..
제가 with를 잘못 알고 있는 것 같은데
그럼 제가 의도한 대로 구현 하려면 어떻게 해야 하나요..?
자답입니다..
일단은 join과 select를 사용하여 처리는 했습니다만 더 좋은 방법이 있다면 알려주세요.
질문을 자세히 보지 못했습니다만, with() 함수는 가장 먼저 써야 합니다.
Model::with()→other()→... // O
Model::other()→with()→... // X
모델에 관계를 설정해두셨으니 Eager Loading을 이용하여 다음과 같이 조회하면 되지 않을까요?
$ATables = App\ATable::with(['log_time' => function ($query) { $query→where('reg_time', '>', 'now()'); }])→get();
엘로퀀트에서 one to one join 연결의 controller에서 select 해오는 방법이 있을까요?
예를 들어서 제가 만들고 싶은 건
select a.*, b.log_time
from a_table as a
inner join b_table as b on a.id = b.id
이런 식의 쿼리를 구현하고 싶습니다.
그래서,
Atable 모델은
public function log_time()
{
return $this→hasOne(Btable::class, 'fk_id', 'id');
}
Atable::where('reg_time','>','now()')→with('log_time') 이런식으로 했는데,
toSql로 찍어보니 join 과 관련된 쿼리는 없더라구요..
제가 with를 잘못 알고 있는 것 같은데
그럼 제가 의도한 대로 구현 하려면 어떻게 해야 하나요..?
자답입니다..
일단은 join과 select를 사용하여 처리는 했습니다만 더 좋은 방법이 있다면 알려주세요.
질문을 자세히 보지 못했습니다만, with() 함수는 가장 먼저 써야 합니다.
Model::with()→other()→... // O
Model::other()→with()→... // X