Home > PHP Framework > Laravel > Laravel queries no longer need to write a lot of if else judgments!

Laravel queries no longer need to write a lot of if else judgments!

藏色散人
Release: 2021-04-08 08:42:09
forward
2489 people have browsed it

The following tutorial column of laravel will introduce to you "Laravel query no longer requires writing a lot of ifelse judgments, just configure it", I hope it will be helpful to friends in need. !

Laravel queries no longer need to write a lot of if else judgments!

Background

The company's main business is developed using the PHP language and the laravel framework. When doing some list filtering queries, the following code that is difficult to maintain often appears:

        //若干代码 根据参数执行不同where
        if (request('has_score')) {
            $article = $article->with(['scores' => function ($query) {
                $query->where('type', self::TYPE);
                $query->with('user');
            }]);
        }
        if (has_module('Audit')) {
            $article = $article->with(['auditing' => function ($query) {
                $query->orderBy('id', 'desc');
            }]);
        }
        $article = $article->with(['videos' => function ($query) {
            $query->where('type', VIDEO);
        }])->with(['audios' => function ($query) {
            $
            query->where('type', AUDIO);
        }]);
Copy after login

Solution

If these queries can be configured and the data is queried according to the configuration instead of judging directly through if in the code, the code will be more elegant. I develop a service package laravel-query-builder myself.

laravel-query-builder is the laravel framework to execute the query condition builder service package based on existing configuration

Install

composer require zyimm/laravelquery-builder
Copy after login

Require

    {
      "require": {
          "php": ">=7.0",
          "fideloper/proxy": "^4.0",
          "laravel/framework": ">=5.5"
        }  
    }
Copy after login

Usage

/**
// 目前支持条件操作符
    '=',
    '',
    '>',
    '>=',
    ' 20,
    'user_id'=> 'zyimm',
    'user_name' => "zyimm,12"
];
//配置数据库字段查询操作
$condition =[
    '=' => [
        'log_id'
    ],
    'not_in' => [
        'user_id'
    ],
    'between' => [
        'user_name'
    ],
    'full_like' => [
        'user_id'
    ],
    '' => [
        'user_id'
    ],
    '>' => [
        'user_id'
    ]
];
DB::enableQueryLog();
//model
\App\Models\Log::query()
    ->where(function ($query) use ($build, $data, $condition){
        $build->buildQueryWhere($data ,$condition, $query);
    })->get();
dd(DB::getQueryLog());
Copy after login

Generate SQL query records as shown below:
Laravel queries no longer need to write a lot of if else judgments!

Tips:
'in','not_in','between' ,'not_between'These identifiers support arrays and character strings. Strings have optional ',' and '.' as delimiters.

It’s so simple that it can make our code more elegant!

Project address

Tip: laravel-query-builderp(https://github.com/zyimm/laravel-query-builder)
Related recommendations:
The latest five Laravel video tutorials

The above is the detailed content of Laravel queries no longer need to write a lot of if else judgments!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template