Giter Site home page Giter Site logo

laravel-query-updater's Introduction

Build Eloquent update queries from put API requests

Quality Score Code Coverage Build Status StyleCI License: MIT Laravel 6.x

This package allows you to update fields and relations based on a request without losing data not send.

Installation

This package can be used in Laravel 6.0 or higher.

You can install the package via composer:

composer require latevaweb/laravel-query-updater

The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:

'providers' => [
    // ...
    LaTevaWeb\QueryUpdater\QueryUpdaterServiceProvider::class,
];

Basic usage

Update model field based on a put or patch request: /users params: ['name' => 'Marc']:

use LaTevaWeb\QueryUpdater\QueryUpdater;

public function __invoke(Request $request, User $user) {
    QueryUpdater::for($user)
        ->updatedFields(['name'])
        ->save();
        
    // update
}

Update model field but keep stored value if parameter is empty or null

use LaTevaWeb\QueryUpdater\QueryUpdater;
use LaTevaWeb\QueryUpdater\KeepStored;

public function __invoke(Request $request, User $user) {
    QueryUpdater::for($user)
        ->updatedFields([
            KeepStored::field('name')
        ])
        ->save();
        
    // update
}

Extending filters

IF you have an special requirement that not fits with our filters KeepStored or SimpleFilter, you can create your own extending LaTevaWeb\QueryUpdater\Filter\AbstractFilter class.

Run tests

sqlite is required. Install it on ubuntu using sudo apt-get install php7.4-sqlite3.

execute vendor/bin/phpunit

License

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

laravel-query-updater's People

Contributors

latevaweb avatar rafatga avatar

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.