All Projects → n7olkachev → Laravel Computed Properties

n7olkachev / Laravel Computed Properties

Make your accessors smarter

Projects that are alternatives of or similar to Laravel Computed Properties

Larapoll
A Laravel package to manage your polls
Stars: ✭ 189 (-4.06%)
Mutual labels:  eloquent, laravel
Laravel Model Expires
A package to assign expiration dates to Eloquent models.
Stars: ✭ 148 (-24.87%)
Mutual labels:  eloquent, laravel
Querybuilderparser
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.
Stars: ✭ 126 (-36.04%)
Mutual labels:  eloquent, laravel
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (-38.58%)
Mutual labels:  eloquent, laravel
Cms
Multilingual PHP CMS built with Laravel and bootstrap
Stars: ✭ 2,342 (+1088.83%)
Mutual labels:  eloquent, laravel
Sieve
A simple, clean and elegant way to filter Eloquent models.
Stars: ✭ 123 (-37.56%)
Mutual labels:  eloquent, laravel
Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (-30.46%)
Mutual labels:  eloquent, laravel
Laravel Settings
Store key value pair in database as settings
Stars: ✭ 107 (-45.69%)
Mutual labels:  eloquent, laravel
Rating
Laravel Eloquent Rating allows you to assign ratings to any model.
Stars: ✭ 175 (-11.17%)
Mutual labels:  eloquent, laravel
Schedule
Schedule is a package that helps tracking schedules for your models. If you have workers in a company, you can set schedules for them and see their availability though the time.
Stars: ✭ 155 (-21.32%)
Mutual labels:  eloquent, laravel
Laravel Invoicable
Easy invoice creation for Laravel
Stars: ✭ 118 (-40.1%)
Mutual labels:  eloquent, laravel
Eloquent Filter
The Eloquent Filter is a package for filter data of models by the query string. Easy to use and fully dynamic.
Stars: ✭ 175 (-11.17%)
Mutual labels:  eloquent, laravel
Searchable
Search/filter functionality for Laravel's Eloquent models
Stars: ✭ 113 (-42.64%)
Mutual labels:  eloquent, laravel
Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
Stars: ✭ 196 (-0.51%)
Mutual labels:  eloquent, laravel
Laravel Cacheable
Rinvex Cacheable is a granular, intuitive, and fluent caching system for eloquent models. Simple, but yet powerful, plug-n-play with no hassle.
Stars: ✭ 107 (-45.69%)
Mutual labels:  eloquent, laravel
Eager Load Pivot Relations
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
Stars: ✭ 134 (-31.98%)
Mutual labels:  eloquent, laravel
Sarala
Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
Stars: ✭ 101 (-48.73%)
Mutual labels:  eloquent, laravel
Laravel Translatable
Making Eloquent models translatable
Stars: ✭ 1,390 (+605.58%)
Mutual labels:  eloquent, laravel
Pinatra
A PHP copy of Sinatra: a DSL for quickly creating web applications in PHP with minimal effort.
Stars: ✭ 151 (-23.35%)
Mutual labels:  eloquent, laravel
Laravel Auditing
Record the change log from models in Laravel
Stars: ✭ 2,210 (+1021.83%)
Mutual labels:  eloquent, laravel

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)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].