1

So, i have a mongo database filled(21k enteries) with columns like action(there are 5 different actions) id, time, etc.

I need to get the name of every action, and how many times does this action occur. For example: USERPROPERTY_CHANGED - 755

I have tried pretty much everything in here Laravel Eloquent groupBy() AND also return count of each group

Then i tried to make another collection, where i input the fields one, by one, and then fetch them, my migration looks like this:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class actionPopularity extends Migration
{
    public function up()
    {
        Schema::create('actionPopularity', function (Blueprint $collection) {
            $collection->Increments('id');
            $collection->string('action');
            $collection->integer('count');
        });
    }
    public function down()
    {
        Schema::dropIfExists('actionPopularity');
    }
}

But the collection that this generated had only one field - id.

heres something that kind of works (it shows that it can work with the database)

Controller:

public function database_test()
{
    $actions = Action::groupBy('action')->get();

    return view('database_test',compact('actions'));
}

View:

{{$actions}} 

Output:

[
    {
        "_id":
        {
            "action":"OBJECT_INSERT"
        },
        "action":"OBJECT_INSERT"
    },
    {
        "_id":
        {
            "action":"OBJECT_MODIFY"
        },
        "action":"OBJECT_MODIFY"

    },
    {
        "_id":
        {
            "action":"null"
        },
        "action":"null"
    },
    {
        "_id":
        {
            "action":"USERPROPERTY_CHANGED"
        },
        "action":"USERPROPERTY_CHANGED"
    },
    {
        "_id":
        {
            "action":"OBJECT_DELETE"
        },
        "action":"OBJECT_DELETE"
    }
]

Ultimately i want to get two arrays, one with action names, and another one with the amount of times that this action has been called, to put it in a chart.

Priyanka khullar
  • 509
  • 1
  • 5
  • 25

2 Answers2

0

Please try the following code:

public function database_test()
{
    $actions = Action::select('action', 'count')->groupBy('action')->get()->toArray();

    return view('database_test',compact('actions'));
}
Dhananjay Kyada
  • 1,015
  • 1
  • 6
  • 14
  • Thank you for responding, this did not work, but i fount a way to generate two arrays, one with the names of actions, and another one with the count of each action. By the way, your code didn't recognize the count statement ..[1] => Array ( [_id] => Array ( [action] => OBJECT_MODIFY ) [action] => OBJECT_MODIFY [count] => ) [2] => Array ( [_id] => Array ( [action] =.... – Artūrs Kovrigo Jun 17 '19 at 12:35
0

This works:


public function database_test()
  {

    $actions = Action::groupBy('action')->get();

    $action_names = array();

    foreach ($actions as $action)
    {
      array_push($action_names, $action->action);
    }

    $action_count = array();

    foreach ($actions as $action)
    {
      $count = Action::where('action','like',$action->action)->count();
      array_push($action_count, $count);
    }

     return view('database_test',compact('actions', 'action_names', 'action_count'));
  }    

view:

{{print_r($action_names)}}

{{print_r($action_count)}}