laravelexport-to-csvoctobercmsoctobercms-backend

How to integrate exporting to csv with backend list in octobercms


I am a beginner of OctoberCMS and I am going to use export feature in backend list. I have read document about importing and exporting in backend of octobercms.

But I am not sure how to use this feature and I want to know followings.

  1. How to add export button on backend list.
  2. How to integrate exporting with backend list.

I hope your kind assistance. Thank you.


Solution

  • Steps [here we need to export TimeLog model data]

    Reference : Tutorialmeta.com

    1. Add this lines to your controller

    // if you have more behaviors add this one as extra
    public $implement = [
        'Backend.Behaviors.ImportExportController',
    ];
    
    // your config
    public $importExportConfig = 'config_import_export.yaml';
    

    2. Now inside your config config_import_export.yaml

    export:
        title: Export TimeLog
        modelClass: HardikSatasiya\TimeTracker\Models\TimeLogExport
        list: $/hardiksatasiya/timetracker/models/timelog/columns.yaml
        redirect: hardiksatasiya/timetracker/timelog
    

    3. $/hardiksatasiya/timetracker/models/timelog/columns.yaml we will use default list model fields so no changes here.


    4. Now you need to put file export.htm in views directory with this content

    <?= Form::open(['class' => 'layout']) ?>
    
        <div class="layout-row">
            <?= $this->exportRender() ?>
        </div>
    
        <div class="form-buttons">
            <button
                type="submit"
                data-control="popup"
                data-handler="onExportLoadForm"
                data-keyboard="false"
                class="btn btn-primary">
                Export records
            </button>
        </div>
    
    <?= Form::close() ?>
    

    5. Now you need any button/ menu which can redirect you to export action of your controller, as we are implementing import-export behavior our controller can have export and import actions.

    Next
    

    so you can point to this url an you can see export screen http://localhost/backend/<author_name>/<plugin_name>/<controller_name>/export OR BETTER you can add export button on toolbar which can redirect you to export screen [use this docu to modify your list toolbar https://octobercms.com/docs/backend/lists#adding-toolbar]

    enter image description here

    If you like more default things

    what you can do is just follow step 1 then in step 2 use this settings

    export:
        useList: true
    

    then you can skip all other config

    last step you just need to point to export url -> http://localhost/backend/<author_name>/<plugin_name>/<controller_name>/export to generate CSV no question asked that url will read all info from list config and let you directly download export.csv file

    for multiple list export and other stuff read this docu: https://octobercms.com/docs/backend/import-export#list-behavior-integration

    if any further questions please add comment