Php 将Laravel Eloquent的with()函数与内部联接一起使用

Php 将Laravel Eloquent的with()函数与内部联接一起使用,php,laravel,join,eloquent,eager-loading,Php,Laravel,Join,Eloquent,Eager Loading,尝试将函数与联接一起使用时遇到问题: $query = Model::query()->with([ 'relationOne', 'relationTwo', ... ]); $query->join(DB::raw("( select * from <models_table> where <some_condition> ) as new_model"), 'new_model.id', '=', '<models_tab

尝试将函数与联接一起使用时遇到问题:

$query = Model::query()->with([
  'relationOne',
  'relationTwo',
  ...
]);

$query->join(DB::raw("(
  select *
  from <models_table>
  where <some_condition>
) as new_model"), 'new_model.id', '=', '<models_table>.id');

$query->paginate($rpp);

在paginate$rpp调用之后,我收到了附加了适当关系的所有项目,但没有连接表aka new_model。有没有办法检索新的\u模型和关系?

请尝试下面的代码,希望对您有所帮助

$query = Model::query()->with([
  'relationOne',
  'relationTwo',
  ...
])

$query = $query->join(DB::raw("(
  select *
  from <models_table>
  where <some_condition>
) as new_model"), 'new_model.id', '=', '<models_table>.id')

$query = $query->paginate($rpp);
您是否尝试添加select语句来强调要获取的表

$query->join(DB::raw("(
  select *
  from <models_table>
  where <some_condition>
) as new_model"), 'new_model.id', '=', '<models_table>.id')
->select(['<models_table>.*', 'new_model.*']);

你想达到什么目标?为什么要使用联接而不是关系和查询生成器?@N69S因为我试图联接的表是“原始”表try add->select'models.*,'new_model.id as new_model_id','new_model.title as new_model_title'…,认为联接在sql中起作用,并返回正确的数据,但是Larvel合并相同的属性名称在分页之前可以转储$query->toSql吗?