Php 如果使用查询生成器,以其laravel的形式是什么?

Php 如果使用查询生成器,以其laravel的形式是什么?,php,postgresql,laravel,laravel-5,subquery,Php,Postgresql,Laravel,Laravel 5,Subquery,我有一个类似下面的sql查询,可能有点复杂,我有点麻烦转换成一个用于laravel的框架。有关framework laravel查询结果的所有信息,请提供帮助。我这里的问题是,我不知道如何为laravel框架创建子选择查询。谢谢各位 SELECT lin_users.status_employee_id, lin_users.id, lin_users.username, lin_users.created, lin_users.modified, lin_employee

我有一个类似下面的sql查询,可能有点复杂,我有点麻烦转换成一个用于laravel的框架。有关framework laravel查询结果的所有信息,请提供帮助。我这里的问题是,我不知道如何为laravel框架创建子选择查询。谢谢各位

SELECT
 lin_users.status_employee_id,
  lin_users.id,
  lin_users.username,
  lin_users.created,
  lin_users.modified,
  lin_employee_attributes.unit_code,
  lin_employee_attributes.position_code,
  lin_employee_attributes.begin_date,
  lin_employee_attributes.end_date,
  contactnumber.contact_id as phone_number,
  contactmobile.contact_id as cell_number,
  contactemail.contact_id as email

FROM lin_users
  INNER JOIN lin_status_employees
    ON lin_users.status_employee_id = lin_status_employees.id

  INNER JOIN lin_people
    ON lin_status_employees.person_id = lin_people.id

  INNER JOIN lin_employee_attributes
    ON lin_users.status_employee_id = lin_employee_attributes.status_employee_id

  LEFT JOIN lin_contacts AS contactnumber
    ON lin_people.id = contactnumber.person_id AND contactnumber.contact_type = 'Work Telephone'

  LEFT JOIN lin_contacts AS contactmobile
    ON lin_people.id = contactmobile.person_id AND contactmobile.contact_type = 'Mobile'

  LEFT JOIN lin_contacts AS contactemail
    ON lin_people.id = contactemail.person_id AND contactemail.contact_type = 'Email'
WHERE lin_employee_attributes.begin_date = '2016-11-07'
      OR lin_employee_attributes.end_date = '2017-10-21'
GROUP BY lin_users.id,
  lin_employee_attributes.unit_code,
  lin_employee_attributes.position_code,
  lin_employee_attributes.begin_date,
  lin_employee_attributes.end_date, lin_people.id,
  contactnumber.contact_id,
  contactmobile.contact_id,
  contactemail.contact_id;
试试这个:

const TABLE = 'my_table_name';

return $this
    ->select(
        self::TABLE . 'id as myidalias',
        self::TABLE . 'username as myuseralias') 
    ->addSelect(DB::raw(
        "
        (your custom select here) as mycustomresult
        "
    ));
您可以添加任意数量的addSelect。另外,当您有很多类似这样的复杂查询时,很多时候都会有重复的部分,因此我强烈建议您使用它,以使代码干净且可重用

public function scopeLeftJoinCategory($query)
{
    return $query
            ->leftJoin(Category::CONTENTS_CATEGORIES . ' AS cc', 'con.id', '=', 'cc.content_id')
            ->leftJoin(Category::TABLE . ' AS cat', 'cc.category_id', '=', 'cat.id');
}

然后您可以这样使用它:
->leftJoinCategory()

使用此工作。最后,用上面的查询可以解决用概念查询到laravel中的查询

$users = $this->db->getTable('users')
            ->select('users.status_employee_id',
                     'users.id',
                     'users.username',
                     'users.email',
                     'users.created',
                     'users.modified',
                     'users.flag_delete',
                     'employee_attributes.unit_code',
                     'employee_attributes.position_code',
                     'employee_attributes.begin_date',
                     'employee_attributes.end_date',
                     'contactnumber.contact_id as phone_number',
                     'contactmobile.contact_id as cell_number',
                     'contactemail.contact_id as email'
                    )
            ->join('status_employees', 'users.status_employee_id', '=', 'status_employees.id')
            ->join('people', 'status_employees.person_id', '=', 'people.id')
            ->join('employee_attributes', 'users.status_employee_id', '=', 'employee_attributes.status_employee_id')
            ->leftJoin('contacts AS contactnumber', function($join)
                     {
                         $join->on('people.id', '=', 'contactnumber.person_id');
                         $join->on('contactnumber.contact_type','=', DB::raw("'Work Telephone'"));
                     })
             ->leftJoin('contacts AS contactmobile', function($join)
                     {
                          $join->on('people.id', '=', 'contactmobile.person_id');
                          $join->on('contactmobile.contact_type','=', DB::raw("'Mobile'"));
                     })
              ->leftJoin('contacts AS contactemail', function($join)
                     {
                         $join->on('people.id', '=', 'contactemail.person_id');
                         $join->on('contactemail.contact_type','=', DB::raw("'Email'"));
                     })

            ->where(function ($query) use ($begin_date, $end_date) {
                $query->where('employee_attributes.begin_date', $begin_date)
                      ->orWhere('employee_attributes.end_date', $end_date);
            })
            ->groupby('users.status_employee_id',
                      'users.id',
                      'users.username',
                      'users.email',
                      'users.created',
                      'users.modified',
                      'users.flag_delete',
                      'employee_attributes.unit_code',
                      'employee_attributes.position_code',
                      'employee_attributes.begin_date',
                      'employee_attributes.end_date',
                      'contactnumber.contact_id',
                      'contactmobile.contact_id',
                      'contactemail.contact_id'
                    )
            ->get();

我建议使用这些原始查询。Laravel的Eloquent是一个查询生成器,它不如优化的原始查询那么有效。我尝试使用该查询,但结果是:
SQLSTATE[42P01]:未定义的表:7错误:关系“users”不存在第1行:select*from users where id=$1^(SQL:select*from users where id=1)
通过数据库中的user表。感谢Zoltán Jére,我尝试实现它。我希望它能解决这个问题。我还建议添加您可以看到最后的查询laravel用您的代码构建了什么。如果查询无法按预期工作,您可以手动复制并运行它,这有助于找到错误的部分。很棒的工具laravel DebugBar,我尝试在项目laravel中安装。