How to get the data from a collection array in Laravel

Related searches

I have this Collection Array in Laravel that is from the database

 public function getdiagnosistally(){

      $jsonData = ListBuilder::all()->pluck('listdata');
        $res = $jsonData->contains('Test');
        dd($jsonData,$res);
}

I was wondering if it is possible to count the occurrences of the following data inside the collection.

I have tried using below contain method but it does not give me a true result even if there is Test inside the Collection.

 $res = $jsonData->contains('Test');

I have also tried using count method but it only gives me the total amount of the collection not the count of Test which is 2 but the result gives me 6.

 $res = $jsonData->count('Test');

The end result i was aiming is like it will be counting the occurrences of Test from the json data.

for example :-

Test = 2
Test child 1 = 4

Would you be able to advice how i can fix it?

Since you have a Collection, you could solve it functionally using filter and array_count_values.

  1. First we decode the json to array and wrap it in a Collection.
  2. Then we filter to keep only the values containing the string 'Test'
  3. We use flatMap to get all these values in a single array
  4. So we can finally use array_count_values to get the desired result.

code:

$testValues = $jsonData->flatMap(function($json) {
    $data = collect(json_decode($json, true)['checklist']);
    return $data->filter(function($item) {
        return str_contains($item, 'Test');
    });
});

$result = array_count_values($testValues->all());

dd($result);

Note that in this solution I assumed the first level of the json is always "checklist". If not, you have to loop over it.

Collections - Laravel, However, collections are much more powerful than arrays and expose a variety Most methods return Illuminate\Database\Eloquent\Collection instances; The except method returns all of the models that do not have the given primary keys: ->toArray() does not convert the collection to an array, it converts the whole contents into arrays, including the items of the collection. ->all() should be the accepted answer. – Sebastien C. Jan 23 at 5:16

You count the occurrences manually by iterating over collection and JSON data converted to an array.

I've just tested this and it will work for the data format you're using:

$count = 0;
$jsonData->map(function($i) use(&$count) {
    $i = json_decode($i, true);
    array_map(function($i) use(&$count) {
        if (str_contains($i, 'Test')) $count++;
    }, $i['checklist']);
});

Eloquent: Collections - Laravel, How would I access [data] inside of this collection? I have tried. Given the plural of notifications is $user->notifications an array? Have you� The Laravel Eloquent is used to get the results from the database, but its return response as a collection. You can convert the collection into an array and work with them, But collection has the number of useful methods that help to manipulate the response data.

For instance your database table name is "Checklist" and "test" is a column.

$count = Checklist::Select('checklist.test')->count();

You can also use where if that's the condition ->where('checklist_id', $request->id)

Access array inside collection?, Is it possible to return a collection of arrays rather than objects from it done every time you fetch results, go to your database.php config file� Getting A Subset Of Data. Given an array of data, and subsequently a collection, you may want to get a section of the it. This could be: The first 2 records; The last 2 records; All the records but in groups of 2. Collections bless us with a few methods that do just that. take. The take method takes an integer value and returns the specified

Eloquent results as array instead of object, Here is my collection (simplified as array): [0] => Array ( [tmdb_id] => 343611 [title] [0] => Array ( [tmdb_id] => 343611 [title] => Jack Reacher: Never Go Back [poster] https://laravel.com/docs/5.3/collections#method-pluck The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models. All collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:

Collections get only specific key value, The above examples show how to access data with the help of Laravel's fluent query builder. If you are using models you can access the data with Laravel's Eloquent ORM Because Eloquent is internaly using the query builder, you can without any problem do the following things:

In this example, we will simple create one route and call controller method. that controller method we will create our custom array and convert into collection object. we also create one paginate() in same controller to create pagination in laravel 6. then you have to just call view and pass result variable. you can use like your paginate object.

Comments
  • That's an array of strings mate. There's no way to check the actual data without using json_decode. Ofc you can hack it by doing something like stristr but that's just silly.
  • In addition to @Andrew's comment: You've got 5 occurrences of "Test", not 2. So even if you are able to fix your code, your results would seem "wrong" to you.
  • You have an array of strings. You just need to count the occurrences. As Andrew pointed out. You must decode the JSON. This is not laravel related but rather a PHP question.
  • thanks for the input, will note them down
  • Thank you winkbrace for the detailed explanation. I'll try the sample code you have provide and I will check how to properly use the filter and array_count_method. Cheers!
  • Thank you! will check this out and try to implement