Serialization for Eloquent's QueryBuilder

Overview

Serialization for Eloquent's QueryBuilder

Installation

composer require anourvalar/eloquent-serialize

Usage

Serialize

$package = \EloquentSerialize::serialize(
    \App\User
        ::with('userPhones')
        ->where('id', '>', '10')
        ->limit(20)
);

Unserialize

$builder = \EloquentSerialize::unserialize($package);

foreach ($builder->get() as $item) {
    // ...
}
Comments
  • You don't need this anymore

    You don't need this anymore

    Love what you done here but laravel forked a very popular package which already can do this, (actually this is just a small part of what it can do) and anyways it is now maintained by laravel itself :

    https://github.com/laravel/serializable-closure

    Give it a test if it works in all your tests, then your package isn't useful anymore.

    opened by Stevemoretz 14
  • I am confused which package to use?

    I am confused which package to use?

    I making package to export unlimited bulky data into CSV, I need to send eloquent query into queue jobs, so its necessary for me to serialize eloquent query, and then unserialize back. User may send any type of query, long, short, with lots of relationships, etc. So I just wanted to ask if this package is capable of handling all types of queries. I found other packages doing such things too: https://github.com/laravie/serialize-queries https://github.com/laravel/serializable-closure

    Please do help me, before I publish the package.

    opened by AkshayGadekar 5
  • Enable \Illuminate\Database\Query\Builder and \Illuminate\Database\Eloquent\Builder

    Enable \Illuminate\Database\Query\Builder and \Illuminate\Database\Eloquent\Builder

    Thanks a lot for this package. It was exactly the feature I needed! :-)

    But I am using the Query\Builder quite generic and therefore had to extend this package for my own.

    My changes are aimed at allowing both eloquent and the query builder to be used, and then serializing only part of the information accordingly.

    Unfortunately I did not manage to run the unit tests (error message: Illuminate\Database\QueryException : could not find driver (SQL: PRAGMA foreign_keys = OFF;)). Do you have a small instruction, what must be done, so that these also run?

    Best regards Sebastian

    Translated with www.DeepL.com/Translator (free version)

    opened by sWalbrun 5
  • nested relation not working with with()

    nested relation not working with with()

    im using 1.2.4 Laravel 5.5.* when i try to get relation like this ModelName::with('relation1') its working but when use this ModelName::with('relation1.relation2') where relation2 is a relation inside Model of relation1 i get this error Call to undefined relationship [relation1.relation2]

    opened by DeaaDarawsheh 3
  • Automatic conversion of false to array is deprecated

    Automatic conversion of false to array is deprecated

    Hey.

    We're running PHP 8.1.5 and Laravel 9.10.0 and getting this deprecation:

    [2022-04-30 14:28:41] pipeline.WARNING: ErrorException: Automatic conversion of false to array is deprecated in /app/vendor/anourvalar/eloquent-serialize/src/Grammars/EloquentBuilderGrammar.php:108
    Stack trace:
    #0 /app/vendor/laravel/framework/src/Illuminate/Support/helpers.php(418): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}()
    #1 /app/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(106): with()
    #2 /app/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(71): Illuminate\Foundation\Bootstrap\HandleExceptions->handleDeprecation()
    #3 /app/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(229): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError()
    #4 /app/vendor/anourvalar/eloquent-serialize/src/Grammars/EloquentBuilderGrammar.php(108): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}()
    #5 /app/vendor/anourvalar/eloquent-serialize/src/Grammars/EloquentBuilderGrammar.php(64): AnourValar\EloquentSerialize\Service->cleanStaticConstraints()
    #6 /app/vendor/anourvalar/eloquent-serialize/src/Grammars/EloquentBuilderGrammar.php(16): AnourValar\EloquentSerialize\Service->getEagers()
    #7 /app/vendor/anourvalar/eloquent-serialize/src/Grammars/ModelGrammar.php(18): AnourValar\EloquentSerialize\Service->packEloquentBuilder()
    #8 /app/vendor/anourvalar/eloquent-serialize/src/Service.php(19): AnourValar\EloquentSerialize\Service->pack()
    #9 /app/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(337): AnourValar\EloquentSerialize\Service->serialize()
    

    To give some context, here are the variables from the debugger:

    $item = false
    $key = {int} 0
    $packedQueryBuilder = {array} [16]
     bindings = {array} [9]
      select = {array} [0]
      from = {array} [0]
      join = {array} [0]
      where = {array} [0]
      groupBy = {array} [0]
      having = {array} [0]
      order = {array} [0]
      union = {array} [0]
      unionOrder = {array} [0]
     aggregate = null
     columns = null
     distinct = false
     from = "subscription_items"
     wheres = {array} [0]
     groups = null
     havings = null
     orders = null
     limit = null
     offset = null
     unions = null
     unionLimit = null
     unionOffset = null
     unionOrders = null
     joins = null
    $packedReferenceQueryBuilder = {array} [16]
     bindings = {array} [9]
      select = {array} [0]
      from = {array} [0]
      join = {array} [0]
      where = {array} [0]
      groupBy = {array} [0]
      having = {array} [0]
      order = {array} [0]
      union = {array} [0]
      unionOrder = {array} [0]
     aggregate = null
     columns = null
     distinct = false
     from = "subscription_items"
     wheres = {array} [0]
     groups = null
     havings = null
     orders = null
     limit = null
     offset = null
     unions = null
     unionLimit = null
     unionOffset = null
     unionOrders = null
     joins = null
    $properties = {array} [14]
     0 = "aggregate"
     1 = "columns"
     2 = "distinct"
     3 = "wheres"
     4 = "groups"
     5 = "havings"
     6 = "orders"
     7 = "limit"
     8 = "offset"
     9 = "unions"
     10 = "unionLimit"
     11 = "unionOffset"
     12 = "unionOrders"
     13 = "joins"
    $property = "distinct"
    $this = {AnourValar\EloquentSerialize\Service} [0]
    $_ENV = {array} [37]
    $_SERVER = {array} [41]
    
    opened by oprypkhantc 2
  • Constaints on multiple hasMany relations of the model broken

    Constaints on multiple hasMany relations of the model broken

    Description:

    If a model A defines two hasMany relationships on model B, each with it's own constraints, and you attempt to eager load both of the relations, it breaks with SQLSTATE[HY093]: Invalid parameter number (SQL: select * from files where type in (1, 2, 3) and files.parent_id in (16) and files.deleted_at is null). The bindings are messed up: [1, 2, 3, 4].

    Steps To Reproduce:

    class File extends Model {}
    
    class User extends Model {
        public function files(): HasMany {
            return $this->hasMany(File::class, 'parent_id')
                ->whereIn('type', [1, 2, 3]);
        }
    
        public function otherFiles(): HasMany {
    	return $this->hasMany(File::class, 'parent_id')
                ->where('type', 4);
        }
    }
    
    \EloquentSerialize::unserialize(
            \EloquentSerialize::serialize(
                User::with(['files', 'otherFiles'])
            )
    )->get();
    

    I've also set up a GitHub repo: https://github.com/oprypkhantc/laravel-has-many-bug/commit/ef2112d811f0a28071c58f7a5cfed4379759d7e4

    opened by oprypkhantc 2
  • Duplication of Eager Loaded Relation's Additional Criteria

    Duplication of Eager Loaded Relation's Additional Criteria

    Running the serialize/unserialize on a Builder instance where with() is being called with a relation that has orderBy() or any other eloquent criteria added in the relation definition causes duplication of that addition to it's query. It runs ok with where clauses, but errors on orderby since duplicates are not allowed.

    Relationship

    public function userRoles(): BelongsToMany
    {
        return $this->belongsToMany(UserRole::class)->orderBy('name');
    }
    

    or

    public function userRoles(): BelongsToMany
    {
        return $this->belongsToMany(UserRole::class)->whereNotNull('name');
    }
    

    Eloquent

    $entries = Privilege::with(['userRoles']);
    $queryBuilder = \EloquentSerialize::serialize($entries);
    $testEntries = \EloquentSerialize::unserialize($queryBuilder);
    ddd($testEntries->get());
    

    Sql Error When Using orderBy() A column has been specified more than once in the order by list. Columns in the order by list must be unique.

    This does work when removed from the relation and added into the with as a closure, but then we lose the global default.

    opened by andrewskm 2
  • Fix phpdocs

    Fix phpdocs

    Thank you for developing and sharing this useful package.

    I made a minor mistake at the previous pull request. I fixed that and made some other minor fixes.

    opened by faytekin 0
Owner
Nick
Nick
The query filter bundle allows you to filter data from QueryBuilder and the Database.

The query filter bundle allows you to filter data from QueryBuilder and the Database. you can filter multiple columns at the same time and also you can filter relation fields with two-level deep and without any join in your query builder.

Bugloos 15 Dec 29, 2022
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.

QueryBuilderParser Status Label Status Value Build Insights Code Climate Test Coverage QueryBuilderParser is designed mainly to be used inside Laravel

Tim Groeneveld 149 Nov 11, 2022
A pure PHP implementation of the MessagePack serialization format / msgpack.org[PHP]

msgpack.php A pure PHP implementation of the MessagePack serialization format. Features Fully compliant with the latest MessagePack specification, inc

Eugene Leonovich 368 Dec 19, 2022
Dubbox now means Dubbo eXtensions, and it adds features like RESTful remoting, Kyro/FST serialization, etc to the Dubbo service framework.

Dubbox now means Dubbo eXtensions. If you know java, javax and dubbo, you know what dubbox is :) Dubbox adds features like RESTful remoting, Kyro/FST

当当 4.9k Dec 27, 2022
The query filter bundle allows you to filter data from QueryBuilder and the Database

The query filter bundle allows you to filter data from QueryBuilder and the Database. you can filter multiple columns at the same time and also you can filter relation fields with two-level deep and without any join in your query builder.

Milad Ghofrani 0 Apr 8, 2022
Opis Closure - a library that aims to overcome PHP's limitations regarding closure serialization

Opis Closure is a library that aims to overcome PHP's limitations regarding closure serialization by providing a wrapper that will make all closures serializable.

Opis 2.4k Dec 18, 2022
The query filter bundle allows you to filter data from QueryBuilder and the Database.

The query filter bundle allows you to filter data from QueryBuilder and the Database. you can filter multiple columns at the same time and also you can filter relation fields with two-level deep and without any join in your query builder.

Bugloos 15 Dec 29, 2022
The query sorting bundle allows you to sort data from QueryBuilder and the Database

The query sorting bundle allows you to sort data from QueryBuilder and the Database. you can sort multiple columns at the same time and also you can sort relation fields with two-level deep and without any join in your query builder.

Bugloos 12 Oct 6, 2022
JSON <=> PHP8+ objects serialization / deserialization library

A simple library for JSON to PHP Objects conversions Often times, we interact with an API, or data source that returns JSON. PHP only offers the possi

Square 90 Dec 20, 2022