You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OctoberCMS后端列表集成CSV导出功能方法咨询

Hey there! I’ve set up OctoberCMS backend list exports plenty of times, so let’s break this down into simple, actionable steps for you—no confusion required.

Step 1: Add the Export Button to Your List Toolbar

First, we’ll add an export button to your backend list’s toolbar. Open your list configuration file (usually located at plugins/yourvendor/yourplugin/controllers/yourrecords/config_list.yaml) and update the toolbar section to include a custom button:

toolbar:
    buttons: list_toolbar
    search:
        prompt: backend::lang.list.search_prompt

list_toolbar:
    -
        label: 'Export Records'
        icon: 'icon-download'
        url: 'yourvendor/yourplugin/yourrecords/export'
        class: 'btn btn-primary'

Don’t forget to replace yourvendor, yourplugin, and yourrecords with your actual vendor, plugin, and controller names!

Step 2: Add the Export Method to Your Backend Controller

Next, open your controller file (e.g., plugins/yourvendor/yourplugin/controllers/YourRecords.php) and add an export method. This method will handle fetching the list data (respecting any filters/sorts the user has applied) and generating the export file:

public function export()
{
    // Create an instance of the list widget to access filtered/sorted data
    $listWidget = $this->createListWidget();
    
    // Get the records based on the current list view
    $data = $listWidget->getRecords();
    
    // Define which columns to export (map database fields to human-readable labels)
    $exportColumns = [
        'id' => 'Record ID',
        'name' => 'Full Name',
        'email' => 'Email Address',
        'created_at' => 'Created Date'
    ];
    
    // Generate and return the export file (default is CSV; you can specify 'xlsx' for Excel)
    return \Backend\Facades\Backend::export(
        $data,
        $exportColumns,
        'your-records-export-' . date('Y-m-d'),
        'csv' // Optional: change to 'xlsx' for Excel format
    );
}

Step 3: Set Up Permissions (Important!)

To make sure only authorized users can access the export feature, add a permission in your plugin’s config_permissions.yaml file:

yourvendor.yourplugin.export_yourrecords:
    label: 'Export Your Records'
    tab: 'Your Plugin Name'

Then, either add this permission to your controller’s $requiredPermissions array:

protected $requiredPermissions = ['yourvendor.yourplugin.export_yourrecords'];

Or use the @permission annotation on the export method:

/**
 * @permission('yourvendor.yourplugin.export_yourrecords')
 */
public function export()
{
    // ... existing code ...
}

Step 4: Optional: Export Only Selected Records

If you want users to export just the records they’ve checked in the list, modify the export method to check for selected IDs:

public function export()
{
    $selectedIds = post('checked');
    
    if ($selectedIds) {
        // Export only checked records
        $data = \YourVendor\YourPlugin\Models\YourRecord::whereIn('id', $selectedIds)->get();
    } else {
        // Export all filtered records
        $listWidget = $this->createListWidget();
        $data = $listWidget->getRecords();
    }
    
    // ... rest of the export code remains the same ...
}

Step 5: Test It Out

Log into your OctoberCMS backend, navigate to your list page, and click the new "Export Records" button. You should get a download prompt for your CSV/Excel file with the correct data. If you run into issues, double-check:

  • Your route URL in the toolbar matches the controller method
  • Permissions are assigned to your user account
  • The column mappings in the export method match your model’s database fields

That’s it! You’ve successfully integrated export functionality into your backend list.

内容的提问来源于stack exchange,提问作者L.Benrong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:52:16