Laravel Convert Array To Collection Example

Today, We want to share with you Laravel Convert Array To Collection Example.In this post we will show you Recursive conversion of array to collection, hear for convert array to Laravel Eloquent Collection we will give you demo and example for implement.In this post, we will learn about laravel convert multidimensional array to collection with an example.

Laravel Convert Array To Collection Example

There are the Following The simple About Laravel Convert Array To Collection Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Convert Array To Collection in Laravel, so the laravel collection to array of objects for this example is following below.

Read Also:  How to Integrate PayPal Payment Gateway in PHP?

Example 1: Convert Array To Collection

Laravel all() Methods

The Laravel all method returns the underlying array represented by the Simple collection:

Syntex

collect([1, 2, 3])->all();

// [1, 2, 3]

Example of the Recursive conversion of array to collection
$collection = collect([
    (object) [
        'website' => 'twitter',
        'url' => 'twitter.com'
    ],
    (object) [
        'website' => 'google',
        'url' => 'google.com'
    ]
]);


$sorted = $collection->sortBy('website');
$sorted->values()->all();


The expected output:
[
     {#769
       +"website": "google",
       +"url": "google.com",
     },
     {#762
       +"website": "twitter",
       +"url": "twitter.com",
     },
]

convert Collections object to array or json in Laravel

convert collections object to array

$status_data = App\Status::all();
return $status_data->toArray();

Convert eloquent model to json :

$status = App\Status::find(1);
return $status->toJson();

Cast Model to String

$status = App\Status::find(1);
return (string) $status;

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  How to Add Captcha in PHP Registration Form?

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Convert Array To Collection Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.