【问题标题】:Laravel select with condition for joinLaravel 选择加入条件
【发布时间】:2015-10-26 12:44:06
【问题描述】:

我的 Laravel 5 项目中有 3 个表:输出、产品和服务。

我想根据这个条件在一个查询中加入这 3 个表:

如果outputs.article_type = 'p' (p = product) 获取 products.name

如果outputs.article_type = 's' (s = service) 获取服务名称

$outputs = Output::join('products', 'products.id', '=', 'outputs.article_id')
                   ... only if outputs.article_type is p (p=product)
                ->join('services', 'services.id', '=', 'outputs.article_id') 
                  ... only if outputs.article_type is s (s=service)
                ->select(array('outputs.output_at','---services or product---.name as article_name'))

【问题讨论】:

    标签: mysql laravel join


    【解决方案1】:

    不确定为什么需要将逻辑放在 one 查询中。似乎是应用 KISS 的完美示例。

    为什么不使用具有 1 个参数和普通旧 IF 的函数来提高可读性并让您(可能还有其他开发人员)的生活更轻松?我认为您尝试做的事情没有任何优势。除了简洁之外,在这种情况下,我认为这不是最重要的因素。

    在 Output.php 模型中:

        /**
         * Retrieves output of the given type.
         *
         * @param  string $type Type of the output
         *
         * @return mixed Object or false in case of problems
         */
        public static function getOutput($type) {
    
            if (!isset($type) || empty($type) || !in_array($type, ['p', 's'])) {
                // Wrong type
                return false;
            }
    
            if ($type === 'p') {
                $baseQuery = Output::join('products', 'products.id', '=', 'outputs.article_id');
                $table = 'products';
            } else {
                $baseQuery = Output::join('services', 'services.id', '=', 'outputs.article_id');
                $table = 'services';
            }
    
            return $baseQuery
                       ->select('outputs.output_at', $table.'.name as article_name')
                       ->get();
        }
    

    然后,在控制器中

    $output = Output::getOutput('p');
    
    if ($output && count($output)) {
        // Do other stuff or pass the data to a view...
    }
    

    请记住,这未经测试,可能需要与我展示的略有不同的实现,但是这个想法仍然很清楚。

    【讨论】:

    • 感谢您的快速回复。但我有一个问题:stackoverflow.com/questions/31781436/…。我无法在使用 add_column 添加的列中搜索我的 DataTables。如果数据来自 SELECT QUERY,我可以这样做。
    • 是的,但那是另一回事 ;)
    【解决方案2】:

    看看A join with additional condition using Query Builder or ELoquent

    但也许您应该考虑预先加载而不是加入?

    Output::with('Product', 'Service')->....
    

    当然你需要先添加关系

    public function service() {
       if ($this->article_type == 's')
           return $this->belongsTo('Service', 'article_id', 'id')
       else
           return null;
     }
    

    并覆盖 name 属性以获取您需要的特定名称

    public functon getNameAttribute() {
       if ($this->service)
           return $this->service->name;
       else if ($this->product)
           return $this->product->name;
    }
    

    【讨论】:

    • 感谢您的回答。但是如何在 QUERY 中调用产品或服务的名称? Output::with('Product', 'Service')->select'
    • @MarioEne 对this 解释有帮助吗?
    • 这是对 1 个表的 WITH QUERY 的一个很好的解释,但我涉及 2 个表:产品和服务。我会尝试从您的链接中推断出来。
    猜你喜欢
    • 2014-11-11
    • 2016-04-12
    • 2020-04-23
    • 1970-01-01
    • 2013-12-25
    • 2017-04-24
    • 1970-01-01
    • 2021-10-22
    • 2018-12-07
    相关资源
    最近更新 更多