Git Product home page Git Product logo

laravel-builder-macros's Introduction

A set of useful Laravel builder macros

Latest Version on Packagist Tests StyleCI Quality Score Total Downloads

The signifly/laravel-builder-macros package allows you to easily add a set of useful builder macros to your Laravel app.

Installation

You can install the package via composer:

composer require signifly/laravel-builder-macros

The package will automatically register itself.

Macros

addSubSelect

Add a select sub query.

// Params: $column, $query
$query->addSubSelect('primary_address_id', 
    Address::select('id')
        ->where('user_id', $user->id)
        ->primary()
);

// It adds primary_address_id to the result set

defaultSelectAll

It selects all columns from the query. Useful for queries with joins and additional selects.

$query->defaultSelectAll()
    ->join('contacts', 'users.id', '=', 'contacts.user_id')
    ->addSelect('contacts.name as contact_name');

joinRelation

A query way to join relations.

// Params: $relationName, $operator
$query->joinRelation('contact');

leftJoinRelation

A query to left join relations.

// Params: $relationName, $operator
$query->leftJoinRelation('contact');

map

A direct method to retrieve the results and map it.

$userIds = $query->where('user_id', 10)->map(function ($user) {
    return $user->id;
});

// Returns a collection

whereLike

Search in your models with the LIKE operator.

$query->whereLike('title', 'john')->get();

// Returns all results where title  includes `john`

You can also supply an array of columns to search in:

$query->whereLike(['title', 'contact.name'], 'john')->get();

// Returns all results where title or contact.name includes `john`

Testing

composer test

Security

If you discover any security issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.