├── .github └── workflows │ └── run-tests.yml ├── .phpunit.result.cache ├── .styleci.yml ├── CHANGELOG.md ├── CODEOWNERS ├── LICENSE.md ├── README.md ├── composer.json └── src ├── BuilderMacroServiceProvider.php └── macros ├── addSubSelect.php ├── defaultSelectAll.php ├── joinRelation.php ├── leftJoinRelation.php ├── map.php └── whereLike.php /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | fail-fast: true 10 | matrix: 11 | os: [ubuntu-latest, windows-latest] 12 | php: [7.4] 13 | laravel: [8.*, 7.*] 14 | dependency-version: [prefer-lowest, prefer-stable] 15 | include: 16 | - laravel: 8.* 17 | testbench: 6.* 18 | - laravel: 7.* 19 | testbench: 5.* 20 | 21 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 22 | 23 | steps: 24 | - name: Checkout code 25 | uses: actions/checkout@v2 26 | 27 | - name: Cache dependencies 28 | uses: actions/cache@v2 29 | with: 30 | path: ~/.composer/cache/files 31 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 32 | 33 | - name: Setup PHP 34 | uses: shivammathur/setup-php@v2 35 | with: 36 | php-version: ${{ matrix.php }} 37 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 38 | coverage: none 39 | 40 | - name: Install dependencies 41 | run: | 42 | composer config --global --auth http-basic.repo.packagist.com token ${{ secrets.COMPOSER_TOKEN }} 43 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 44 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 45 | - name: Execute tests 46 | run: vendor/bin/phpunit -------------------------------------------------------------------------------- /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | C:37:"PHPUnit\Runner\DefaultTestResultCache":44:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:0:{}}} -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-builder-macros` will be documented in this file 4 | 5 | 6 | ## 1.4.1 - 2020-09-11 7 | 8 | - Add support for Laravel 8 9 | 10 | ## 1.0.0 - 2018-08-08 11 | 12 | - Initial release! 13 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @Androlax2 -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Signifly 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A set of useful Laravel builder macros 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/signifly/laravel-builder-macros.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-builder-macros) 4 | ![Tests](https://github.com/signifly/laravel-builder-macros/workflows/Tests/badge.svg) 5 | [![StyleCI](https://styleci.io/repos/144017418/shield?branch=master)](https://styleci.io/repos/144017418) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/signifly/laravel-builder-macros.svg?style=flat-square)](https://scrutinizer-ci.com/g/signifly/laravel-builder-macros) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/signifly/laravel-builder-macros.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-builder-macros) 8 | 9 | The `signifly/laravel-builder-macros` package allows you to easily add a set of useful builder macros to your Laravel app. 10 | 11 | ## Installation 12 | 13 | You can install the package via composer: 14 | 15 | ```bash 16 | composer require signifly/laravel-builder-macros 17 | ``` 18 | 19 | The package will automatically register itself. 20 | 21 | ## Macros 22 | 23 | - [`addSubSelect`](#addSubSelect) 24 | - [`defaultSelectAll`](#defaultSelectAll) 25 | - [`joinRelation`](#joinRelation) 26 | - [`leftJoinRelation`](#leftJoinRelation) 27 | - [`map`](#map) 28 | - [`whereLike`](#whereLike) 29 | 30 | ### `addSubSelect` 31 | 32 | Add a select sub query. 33 | 34 | ```php 35 | // Params: $column, $query 36 | $query->addSubSelect('primary_address_id', 37 | Address::select('id') 38 | ->where('user_id', $user->id) 39 | ->primary() 40 | ); 41 | 42 | // It adds primary_address_id to the result set 43 | ``` 44 | 45 | ### `defaultSelectAll` 46 | 47 | It selects all columns from the query. Useful for queries with joins and additional selects. 48 | 49 | ```php 50 | $query->defaultSelectAll() 51 | ->join('contacts', 'users.id', '=', 'contacts.user_id') 52 | ->addSelect('contacts.name as contact_name'); 53 | ``` 54 | 55 | ### `joinRelation` 56 | 57 | A query way to join relations. 58 | 59 | ```php 60 | // Params: $relationName, $operator 61 | $query->joinRelation('contact'); 62 | ``` 63 | 64 | ### `leftJoinRelation` 65 | 66 | A query to left join relations. 67 | 68 | ```php 69 | // Params: $relationName, $operator 70 | $query->leftJoinRelation('contact'); 71 | ``` 72 | 73 | ### `map` 74 | 75 | A direct method to retrieve the results and map it. 76 | 77 | ```php 78 | $userIds = $query->where('user_id', 10)->map(function ($user) { 79 | return $user->id; 80 | }); 81 | 82 | // Returns a collection 83 | ``` 84 | 85 | ### `whereLike` 86 | 87 | Search in your models with the `LIKE` operator. 88 | 89 | ```php 90 | $query->whereLike('title', 'john')->get(); 91 | 92 | // Returns all results where title includes `john` 93 | ``` 94 | 95 | You can also supply an array of columns to search in: 96 | ```php 97 | $query->whereLike(['title', 'contact.name'], 'john')->get(); 98 | 99 | // Returns all results where title or contact.name includes `john` 100 | ``` 101 | 102 | ## Testing 103 | ```bash 104 | composer test 105 | ``` 106 | 107 | ## Security 108 | 109 | If you discover any security issues, please email dev@signifly.com instead of using the issue tracker. 110 | 111 | ## Credits 112 | 113 | - [Morten Poul Jensen](https://github.com/pactode) 114 | - [All contributors](../../contributors) 115 | 116 | ## License 117 | 118 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 119 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "signifly/laravel-builder-macros", 3 | "description": "A set of useful Laravel builder macros.", 4 | "homepage": "https://github.com/signifly/laravel-builder-macros", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Morten Poul Jensen", 9 | "email": "mpj@signifly.com", 10 | "role": "Developer" 11 | } 12 | ], 13 | "require": { 14 | "php": "^7.2.5|^8.0", 15 | "illuminate/database": "^6.0|^7.0|^8.0|^9.0|^10.0", 16 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^7.0|^8.0|^9.0", 20 | "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Signifly\\BuilderMacros\\": "src" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Signifly\\BuilderMacros\\Test\\": "tests" 30 | } 31 | }, 32 | "scripts": { 33 | "test": "vendor/bin/phpunit" 34 | }, 35 | "config": { 36 | "sort-packages": true 37 | }, 38 | "extra": { 39 | "laravel": { 40 | "providers": [ 41 | "Signifly\\BuilderMacros\\BuilderMacroServiceProvider" 42 | ], 43 | "aliases": { 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/BuilderMacroServiceProvider.php: -------------------------------------------------------------------------------- 1 | mapWithKeys(function ($path) { 14 | return [$path => pathinfo($path, PATHINFO_FILENAME)]; 15 | }) 16 | ->each(function ($macro, $path) { 17 | require_once $path; 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/macros/addSubSelect.php: -------------------------------------------------------------------------------- 1 | defaultSelectAll(); 7 | 8 | return $this->selectSub($query->limit(1)->getQuery(), $column); 9 | }); 10 | -------------------------------------------------------------------------------- /src/macros/defaultSelectAll.php: -------------------------------------------------------------------------------- 1 | getQuery()->columns)) { 7 | $this->select($this->getQuery()->from.'.*'); 8 | } 9 | 10 | return $this; 11 | }); 12 | -------------------------------------------------------------------------------- /src/macros/joinRelation.php: -------------------------------------------------------------------------------- 1 | getRelation($relationName); 7 | 8 | return $this->join( 9 | $relation->getRelated()->getTable(), 10 | $relation->getQualifiedForeignKeyName(), 11 | $operator, 12 | $relation->getQualifiedOwnerKeyName() 13 | ); 14 | }); 15 | -------------------------------------------------------------------------------- /src/macros/leftJoinRelation.php: -------------------------------------------------------------------------------- 1 | getRelation($relationName); 7 | 8 | return $this->leftJoin( 9 | $relation->getRelated()->getTable(), 10 | $relation->getQualifiedForeignKeyName(), 11 | $operator, 12 | $relation->getQualifiedParentKeyName() 13 | ); 14 | }); 15 | -------------------------------------------------------------------------------- /src/macros/map.php: -------------------------------------------------------------------------------- 1 | get()->map($callback); 7 | }); 8 | -------------------------------------------------------------------------------- /src/macros/whereLike.php: -------------------------------------------------------------------------------- 1 | where(function (Builder $query) use ($columns, $value) { 9 | foreach (Arr::wrap($columns) as $column) { 10 | $query->when( 11 | Str::contains($column, '.'), 12 | 13 | // Relational searches 14 | function (Builder $query) use ($column, $value) { 15 | $parts = explode('.', $column); 16 | $relationColumn = array_pop($parts); 17 | $relationName = join('.', $parts); 18 | 19 | return $query->orWhereHas( 20 | $relationName, 21 | function (Builder $query) use ($relationColumn, $value) { 22 | $query->where($relationColumn, 'LIKE', "%{$value}%"); 23 | } 24 | ); 25 | }, 26 | 27 | // Default searches 28 | function (Builder $query) use ($column, $value) { 29 | return $query->orWhere($column, 'LIKE', "%{$value}%"); 30 | } 31 | ); 32 | } 33 | }); 34 | 35 | return $this; 36 | }); 37 | --------------------------------------------------------------------------------