d

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567
ouroffice@aware.com

 

KMF

Serialization for Eloquent’s QueryBuilder | Laravel News

Eloquent Serialize is a Laravel package to serialize and unserialize Eloquent query builder objects. The EloquentSerialize service has two methods, serialize and unserialize.

Given the following simple query, you can serialize the builder results:

1$data = EloquentSerialize::serialize(

2 AppModelsPost::with('author')->limit(1)

3);

4 

5// => "O:36:"AnourValarEloquentSerializePackage":1:{s:42:"AnourValarEloquentSerializePackagedata";a:4:{s:5:"model";s:15:"AppModelsPost";s:10:"connection";N;s:8:"eloquent";a:3:{s:4:"with";a:1:{s:6:"author";a:16:{s:8:"bindings";a:9:{s:6:"select";a:0:{}s:4:"from";a:0:{}s:4:"join";a:0:{}s:5:"where";a:0:{}s:7:"groupBy";a:0:{}s:6:"having";a:0:{}s:5:"order";a:0:{}s:5:"union";a:0:{}s:10:"unionOrder";a:0:{}}...

From serialized data, you can get back an Eloquent builder instance:

1EloquentSerialize::unserialize($data)->get();

2 

3foreach ($builder->get() as $item) {

4 // ...

5}

6 

7/*

8=> IlluminateDatabaseEloquentCollection {#4430

9 all: [

10 AppModelsPost {#4423

11 id: 1,

12 author_id: 1,

13 title: "Hello World",

14 body: "This is a test post.",

15 created_at: "2021-10-05 04:14:31",

16 updated_at: "2021-10-05 04:14:31",

17 author: AppModelsAuthor {#3483

18 id: 1,

19 name: "Paul Redmond",

20 created_at: "2021-10-05 04:13:36",

21 updated_at: "2021-10-05 04:13:36",

22 },

23 },

24 ],

25*/

This package could be helpful if you need to cache or store a query for users as serialized data and later need to retrieve the results back into an Eloquent builder object.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Credit: Source link

Previous Next
Close
Test Caption
Test Description goes like this