Php 如何在laravel..中使用with()方法连接两个表。。?

Php 如何在laravel..中使用with()方法连接两个表。。?,php,laravel,Php,Laravel,我必须从两个表中提取数据。 我不想使用DB或leftjoin方法。 我希望以以下方式获取数据: User::with('userprofile')->where('is_active','=','1')->get(); 我必须获取用户名,它存储在userprofile表中,用户的状态存储在user表中 谢谢问题解决了,我使用join代替with,现在它工作正常。 例如: 谢谢您也可以通过以下方式获得结果: User::with(['userprofile' => functi

我必须从两个表中提取数据。 我不想使用DB或leftjoin方法。 我希望以以下方式获取数据:

User::with('userprofile')->where('is_active','=','1')->get();
我必须获取用户名,它存储在userprofile表中,用户的状态存储在user表中


谢谢

问题解决了,我使用join代替with,现在它工作正常。 例如:


谢谢

您也可以通过以下方式获得结果:

User::with(['userprofile' => function($query){
   $query->where('is_active','=','1');
}])->get(); 

那么,以上哪一项不起作用呢?我觉得很好,然后你可以循环结果并获取每个用户的用户名,例如$user->userprofile->username或任何你称之为的名称。你阅读了文档了吗?这是我的原始查询:user::with'userprofile'->select'id',DB::raw'CONCATfirst_name',email AS userData'->where'is_active','=',1'->orderBy'email','asc'->列出'userData'、'user.id';这将导致在第一个\u名称中找不到列。
User::with(['userprofile' => function($query){
   $query->where('is_active','=','1');
}])->get();