Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将原始sql查询转换为laravel雄辩查询_Php_Mysql_Laravel_Eloquent_Laravel Query Builder - Fatal编程技术网

Php 将原始sql查询转换为laravel雄辩查询

Php 将原始sql查询转换为laravel雄辩查询,php,mysql,laravel,eloquent,laravel-query-builder,Php,Mysql,Laravel,Eloquent,Laravel Query Builder,我有一个很好的原始查询,但我不能把它翻译成laravel雄辩的 这是我的桌子: 用户表格 客户表格 雇员表 用户模型 我的问题是: 如果我尝试使用原始查询,通过DB::raw(),它将返回一个 数组,我无法对结果分页或排序 我找不到一种方法来从几个表中进行“选择”,以雄辩的方式 我不知道如何从数组中提取数据并获取集合 我仍然不确定我是否做对了 那么,有没有一种方法或其他方法可以使这项工作 编辑: 明确地说,我想得到的是: 用户的集合,包含: 是用户8的“客户”的用户 是用户8的“雇员”的用户

我有一个很好的原始查询,但我不能把它翻译成laravel雄辩的

这是我的桌子:

用户表格 客户表格 雇员表 用户模型 我的问题是:
  • 如果我尝试使用原始查询,通过
    DB::raw()
    ,它将返回一个 数组,我无法对结果分页或排序
  • 我找不到一种方法来从几个表中进行“选择”,以雄辩的方式
  • 我不知道如何从数组中提取数据并获取集合
  • 我仍然不确定我是否做对了
  • 那么,有没有一种方法或其他方法可以使这项工作


    编辑: 明确地说,我想得到的是:

    用户的集合,包含:

    • 是用户8的“客户”的用户
    • 是用户8的“雇员”的用户
    • 用户8

    我可以在上面应用最旧的('seen')->最新的()->paginate($n)或类似的东西。

    看起来您的员工模型设置正确,这应该使这相当简单

    我认为,与简单地尝试将查询转换为使用查询生成器相比,更容易考虑您正在尝试做什么以及雄辩如何帮助您做到这一点

    $id = 8;
    $users = App\User::whereHas('clients', function($q) use ($id) {
        $q->where('id_marchand', $id);
    })->orWhereHas('employes', function($q) use ($id) {
        $q->where('id_marchand', $id);
    })->orWhere('id', $id)
    ->orderBy('seen')
    ->oldest()
    ->get();
    
    这将返回
    用户
    模型的集合。如果要分页,只需将
    get()
    paginate($numRecords)
    交换,其中$numRecords是每页所需的记录数

    然后,通过模型集合,您可以使用foreach循环输出每个用户的数据

    foreach ($users as $user) {
        echo 'email: ' . $user->email;
    }
    
    编辑:我错了,我没有仔细观察模型。因此,在查询中,您分别通过
    id\u client
    id\u employe
    列连接clients和employes表。因此,如果您修改
    用户
    模型,并将
    id\u marchand
    更改为
    id\u employe
    ,用于
    employes
    函数,将
    id\u client
    更改为
    clients
    函数,则此代码应该可以工作(至少对我来说是这样)

    为了澄清,上面的代码生成了以下查询,以便您可以在进行任何更改之前亲自查看结果

    SELECT 
        * 
    FROM
        `users` 
    WHERE EXISTS 
        (SELECT 
        * 
        FROM
        `clients` 
        WHERE `clients`.`id_client` = `users`.`id` 
        AND `id_marchand` = '8') 
        OR EXISTS 
        (SELECT 
        * 
        FROM
        `employes` 
        WHERE `employes`.`id_employe` = `users`.`id` 
        AND `id_marchand` = '8') 
        OR `id` = '8' 
    ORDER BY `seen` ASC,
        `created_at` ASC 
    

    谢谢你的回答,但那不是我要问的。我将得到一个分类的用户集合:所有用户都是用户8的客户,所有用户都是用户8的雇员,还有用户8自己……我将用一些测试数据设置表格,看看问题出在哪里。Alleluia!我已经发疯两周了,我尝试了所有可能的查询。。。非常感谢。
    <?php namespace App\Models;
    
    /**
     * One to Many relation
     *
     * @return Illuminate\Database\Eloquent\Relations\hasMany
     */
    public function employes()
    {
        return $this->hasMany('App\Models\Employe', 'id_marchand');
    }
    
    /**
     * One to Many relation
     *
     * @return Illuminate\Database\Eloquent\Relations\hasMany
     */
    public function clients()
    {
        return $this->hasMany('App\Models\Client', 'id_marchand');
    }
    
    <?php namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Client extends Model
    {
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'clients';
    
        /**
         * One to Many relation
         *
         * @return Illuminate\Database\Eloquent\Relations\BelongsTo
         */
        public function user() 
        {
            return $this->belongsTo('App\Models\User');
        }
    }
    
    <?php namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Employe extends Model
    {
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'employes';
    
        /**
         * One to Many relation
         *
         * @return Illuminate\Database\Eloquent\Relations\BelongsTo
         */
        public function user() 
        {
            return $this->belongsTo('App\Models\User');
        }
    }
    
    SELECT users.* 
    FROM clients, users 
    WHERE clients.id_marchand = 8 
    AND users.id = clients.id_client 
    UNION 
    SELECT users.* 
    FROM employes, users 
    WHERE employes.id_marchand = 8 
    AND users.id = employes.id_employe 
    UNION 
    SELECT users.*
    FROM users
    WHERE users.id = 8
    ORDER BY `seen` ASC, `created_at` DESC 
    LIMIT 25 OFFSET 0
    
    $id = 8;
    $users = App\User::whereHas('clients', function($q) use ($id) {
        $q->where('id_marchand', $id);
    })->orWhereHas('employes', function($q) use ($id) {
        $q->where('id_marchand', $id);
    })->orWhere('id', $id)
    ->orderBy('seen')
    ->oldest()
    ->get();
    
    foreach ($users as $user) {
        echo 'email: ' . $user->email;
    }
    
    SELECT 
        * 
    FROM
        `users` 
    WHERE EXISTS 
        (SELECT 
        * 
        FROM
        `clients` 
        WHERE `clients`.`id_client` = `users`.`id` 
        AND `id_marchand` = '8') 
        OR EXISTS 
        (SELECT 
        * 
        FROM
        `employes` 
        WHERE `employes`.`id_employe` = `users`.`id` 
        AND `id_marchand` = '8') 
        OR `id` = '8' 
    ORDER BY `seen` ASC,
        `created_at` ASC