Coder Social home page Coder Social logo

laravel-computed-properties's Introduction

Computed properties for Eloquent

Code quality Latest Version on Packagist Licence Build Status

Laravel 5.4+

Based on this tweet: https://twitter.com/reinink/status/899713609722449920

Some examples for better understanding of this power:

class Order extends Model
{
    use ComputedProperties;

    public function products()
    {
        return $this->hasMany(OrderProduct::class);
    }

    public function computedSum($order)
    {
        return OrderProduct::select(new Expression('sum(price * count)'))
            ->where('order_id', $order->id);
    }
}

Now, we can get order sum with $order->sum. Yep, we can get this functionality with getSumAttribute but wait! The real power of this package is that we can use this method inside our queries:

$orders = Order::withComputed('sum')->get()

We eager loaded sum attribute without N+1 problem.

But there is more! You can add having or orderBy clauses to such queries for filtering and sorting!

Order::withComputed('sum')->orderBy('sum', 'desc')->get()

Installation

You can install the package via composer:

composer require n7olkachev/laravel-computed-properties

Next, add ComputedProperties trait to your models:

use ComputedProperties;

That's all!

More examples

class Page extends Model
{
    use ComputedProperties;

    public $timestamps = false;

    protected $casts = [
        'last_view' => 'datetime',
        'first_view' => 'datetime',
    ];

    public function computedLastView($page)
    {
        return PageView::select(new Expression('max(viewed_at)'))
            ->where('page_id', $page->id);
    }

    public function computedFirstView($page)
    {
        return PageView::select(new Expression('min(viewed_at)'))
            ->where('page_id', $page->id);
    }
}

We can find Page by its first view:

$page = Page::withComputed('first_view')
    ->having('first_view', Carbon::create(2017, 8, 16, 0, 0, 0))
    ->first();

Or by both first_view and last_view

$page = Page::withComputed(['first_view', 'last_view'])
    ->having('first_view', Carbon::create(2017, 8, 16, 0, 0, 0))
    ->having('last_view', Carbon::create(2017, 8, 21, 0, 0, 0))
    ->first();

We can order pages by theirs last_view

$pages = Page::withComputed('last_view')
    ->orderBy('last_view', 'desc')
    ->get()

Testing

$ composer test

Credits

Sponsored by

https://websecret.by/

Web agency based in Minsk, Belarus

License

The MIT License (MIT)

laravel-computed-properties's People

Contributors

ankurk91 avatar n7olkachev avatar tabuna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

laravel-computed-properties's Issues

Benefit of this package vs simple cacher

How is this package any better than just doing something like the below:

public function getSumAttribute()
{    
    return $this->rememberAs('sum', function() {
        OrderProduct::select(new Expression('sum(price * count)'))
            ->where('order_id', $this->id);
    });
}

If you have a basic implementation of rememberAs on your model which sees if the key already exists and if not, run the closure and set the value on the model.

This isn't 'real' computed properites

I assume the computed name has come from the world of Vue.

Computed properties in Vue react to data that changes on the model, so if you were to update anything in which the computed property used, it will re-compute the computed property.

This is more of a 'watch out' type warning to people thinking it's 'real' computed properties (as they know it) from Vue.

Laravel 5.6 still not working

composer require n7olkachev/laravel-computed-properties

Using version ^1.1 for n7olkachev/laravel-computed-properties
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Conclusion: remove laravel/framework v5.6.26
- Conclusion: don't install laravel/framework v5.6.26
- n7olkachev/laravel-computed-properties v1.1.0 requires illuminate/config ~5.4.0 -> satisfiable by illuminate/config[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- n7olkachev/laravel-computed-properties v1.1.1 requires illuminate/config ~5.4.0|~5.5.0 -> satisfiable by illuminate/config[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v
5.5.36, v5.5.37, v5.5.39, v5.5.40].
- don't install illuminate/config v5.4.0|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.4.13|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.4.17|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.4.19|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.4.27|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.4.36|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.4.9|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.0|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.16|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.17|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.2|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.28|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.33|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.34|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.35|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.36|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.37|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.39|don't install laravel/framework v5.6.26
- don't install illuminate/config v5.5.40|don't install laravel/framework v5.6.26
- Installation request for laravel/framework (locked at v5.6.26, required as 5.6.*) -> satisfiable by laravel/framework[v5.6.26].
- Installation request for n7olkachev/laravel-computed-properties ^1.1 -> satisfiable by n7olkachev/laravel-computed-properties[v1.1.0, v1.1.1].

Installation failed, reverting ./composer.json to its original content.

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.