jsadways/operationrecord

There is no license information available for the latest version (1.0.20) of this package.

Installs: 201

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/jsadways/operationrecord

1.0.20 2026-01-08 06:44 UTC

README

step1 use docker-compose set up a MongoDB Server

version: "3.3"

services:
  mongo:
    container_name: mongo
    image: mongo:7.0.12
    restart: unless-stopped
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=123456
    ports:
      - 27017:27017
    volumes:
      - mongo-data:/data/db
    networks:
      - dev
networks:
  dev:
volumes:
  mongo-data:

step2 install package

composer require jsadways/operationrecord

step3 : edit config/database.php

'connections' => [
    ...
    'mongodb' => [
        'driver' => 'mongodb',
        'dsn' => env('MONGO_DB_URI'),
        'database' => env('MONGO_DB_DATABASE', 'forge'),
    ]
    ...
]

step4: add element in .env file

MONGO_DB_URI=mongodb://admin:123456@SERVER_LOCATION
MONGO_DB_DATABASE=YOUR_DB_NAME

Support functions and arguments

  1. set(SetDto) : set data to MongoDB collection
  2. get(array $filter) : get one document form MongoDB collection
  3. list(ListDto) : get list of document form MongoDB collection
  • SetDto : Object contains $data_id,$creator_id,$action_name,$data(*optional)
  • ListDto : Object contains $filter,$sort_by,$sort_order,$per_page,$show_diff
  • array filter : check filter usage from JsAdways/scopeFilter

Usage

create a model

  • this mode describes how to store records to MongoDB
  • use artisan command : php artisan make:operation-record Record --table=example_record
  • the result will be :
#app/Models/ExampleRecord.php

<?php

namespace App\Models;

use Jsadways\Operationrecord\Models\RecodeModel;

class ExampleRecord extends RecodeModel
{
    protected $table = 'example_record';
}

you can use these artisan below

php artisan make:operation-record Record --table=example_record
php artisan make:operation-record Record --table=example_record --namespace=App\\Models
php artisan make:operation-record Record --table=example_record --force

use service in your controller

use Jsadways\Operationrecord\Services\OperationRecordService;
use Jsadways\Operationrecord\Services\ListDto;
use Jsadways\Operationrecord\Services\SetDto;
use App\Models\ExampleRecord; // the custom model class to accrss MongoDB

Use Examples

Set one record with data_id

$record = new OperationRecordService(ExampleRecord::class);
$array_data = [
    'id' => 1,
    'name' => 'alvin'
];
$data = new SetDto(
    data_id: 133,
    data_source: 'employee'
    creator_id: 155,
    data: $array_data,
    memo: 'test data'
);
$result = $record->create_action()->set($data);

Set one record without data_id(will be auto take $array_data['id])

$record = new OperationRecordService(ExampleRecord::class);
$array_data = [
    'id' => 1,
    'name' => 'alvin'
];
$data = new SetDto(
    data_source: 'employee'
    creator_id: 155,
    data: $array_data,
    memo: 'test data'
);
$result = $record->create_action()->set($data);

using facade set record

$array_data = [
    'id' => 1,
    'name' => 'alvin'
];

OperationRecord::for(ExampleRecord::class)
  ->creation_action()
  ->set(new SetDto(
            data_source: 'employee',
            creator_id: 155,
            data: $array_data,
            memo: 'test data'
        ))

using traits in BaseController

class Controller extends BaseController
{
    use RecordsOperation;
    
    protected function getCreatorId(): int|string
    {
        return 172;
    }

    protected function getRecordModel(): string
    {
        return ExampleRecord::class;
    }
}

class BusinessController extends Controller
{
    public function create(Request $request)
    {
        $array_data = [
            'id' => 1,
            'name' => 'alvin'
        ];
        $this->recordOperation(
            action:ActionName::Create,
            data:$array_data,
            memo:'test data'
        );
    }
}

Get one record

$record = new OperationRecordService(ExampleRecord::class);
$filter = [
    'creator_id_eq' => 155,//to get record where creator_id = 155
];
$result = $record->get($filter);

Get list of records

$record = new OperationRecordService(ExampleRecord::class);
$filter = new ListDto(
    filter: ['creator_id_eq'=>2],
    per_page:0, //per_page set 0 do not create pagination object
    show_fidd:true
);
$result = $record->list($filter);

using facade read record

OperationRecord::for(ExampleRecord::class)
  ->list(new ListDto(
            filter: ['creator_id_eq' => 123],
            per_page: 10,
            page: 1,
            show_diff: false
        ))

using traits in BaseController

class Controller extends BaseController
{
    use ReadRecords;

    protected function getRecordModel(): string
    {
        return ExampleRecord::class;
    }
}

class BusinessController extends Controller
{
    public function Read(Request $request)
    {
        $this->readRecord(
            filter: ['creator_id_eq' => 123],
            sort_by: 'id',
            sort_order: 'desc',
            per_page: 10,
            page: 1,
            show_diff: false
        );
    }
}