This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

floriskoch
floriskoch

I'm trying to export all records from a table, called ingredients, from a custom plugin. I've followed the tutorial here: https://octobercms.com/docs/backend/import-export, but I can't get it to work properly. The problem is that all fields are exported fine, except for the related fields, which stay empty.

My export model class looks as follows:

use [namespace]\[namespace]\Models\Ingredient;

class IngredientsExport extends ExportModel
{
    public function exportData($columns, $sessionKey = null)
    {
        $ingredients = Ingredient::all();
        $ingredients->each(function($ingredient) use ($columns) {
            $ingredient->addVisible($columns);
        });

        return $ingredients->toArray();
    }
}

In the Ingredient model I've added the relation and $visible property like so:

protected $visible = ['application', 'chemical', 'product'];

public $belongsTo = [
    'chemical' => ['[namespace]\[namespace]\Models\Chemical'],
    'application' => ['[namespace]\[namespace]\Models\Application'],
    'product' => ['[namespace]\[namespace]\Models\Product'],
];

I also tried changing the $visible variable to ['application.name'] but that doesn't work either.

Apparently it should be really easy to achieve, see: https://github.com/octobercms/october/issues/1313. But I really can't get it to work and I can't find any documentation on im/exporting with relations either.

Any ideas? Thanks!

sekar.nat14872
sekar.nat14872

Exporting a related model data (ie) 'hasMany related field' not able to fetch. I was looking the OC Blog plugin code. There is an exportModel has all the relation inside and used ->get->toArray(). I tried all these method but not success in getting related data.

any update on exporting related field especially hasMany relation? please advise.

kai20720
kai20720

I need this too and Im looking forward to hints :)

Not sure but perhaps this helps?

https://github.com/octobercms/october/issues/1313

Last updated

dima675027198
dima675027198
public function exportData($columns, $sessionKey = null)
    {
        $subscribers = PollItem::withCount('votes')->get();
        $subscribers->each(function($subscriber) use ($columns) {
            $subscriber->addVisible($columns);
        });
        return $subscribers->toArray();
    }

Last updated

mesinrusak
mesinrusak

sorry if it's so late, try this:

class IngredientsExport extends ExportModel {
    public $table = 'ingredients_table';

    public $belongsTo = [
        'chemical' => ['[namespace]\[namespace]\Models\Chemical'],
        'application' => ['[namespace]\[namespace]\Models\Application'],
        'product' => ['[namespace]\[namespace]\Models\Product'],
    ];

    protected $appends = [
        'chemical_name',
        'application_name',
        'product_name',
    ];

    public function exportData($columns, $sessionKey = null) {
        $query = self::make();
        /*
        you can filter anything in here
        */
        return $query->get()->toArray();
    }

    public function getChemicalNameAttribute() {
        return $this->chemical->name;
    }

    public function getApplicationNameAttribute() {
        return $this->application->name;
    }

    public function getProductNameAttribute() {
        return $this->product->name;
    }
}

export.yaml

 . . . 
chemical_name: 'Chemical'
application_name: 'Application'
product_name: 'Product'
 . . .
rupert24306
rupert24306

That helped me, thanks!!

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.