dimanche 30 avril 2017

Search by custom properties of Spatie Activity-log in laravel

I am using ActivityLog for logging my user's activities in Laravel. This package works really well in storing all the create, update and delete activities performed by the user on various models that I have setup with this package.

But there is one issue that I am facing now. I have the ActivityLog table like below-

id | log_name | description | subject_id | subject_type | causer_id | causer_type | properties
----------------------------------------------------------------------------------------------------------------------------------------
7  | default  | created     |    4       | App\Response |   1       | App\User    |{"project_id":"22295","feature_id":"2","part_id":"1","response":"yes","user_id":1}

I need to get the results of this table filtered by project_id stored in the properties column. The package documentation says that Activity is a normal Eloquent Model so we can use all the Eloquent provided default functions to use with the Activity model. But I am not sure how to achieve this with Eloquent?

Right now, I am achieving this by the code below-

$activities = Activity::latest()->get()->filter(function($item) use($projectId) {
    $properties = $item->properties->toArray();
    if(isset($properties['attributes'])) 
        $properties = $properties['attributes'];

    return ($properties['project_id'] == $projectId);
});

The problem with my above code is that it fetches all the activities logged till date, so its loading all the Activity models which exists and then filters through based on the project_id. This will take alot of time when the size of the ActivityLog with increase to a large number, say 100,000 rows. Does anyone know a better way of fetching the filtered results based on project_id without going through all the rows manually?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire