F
Filament10mo ago
ejoi8

Export action to display data from relationships

Hi.. I have follow the documentation to display data from relation for the Export action (https://filamentphp.com/docs/3.x/actions/prebuilt-actions/export#displaying-data-from-relationships). The problem is whe I use ExportColumn::make('gender.name'), the relation value is not showing. but if I just use the gender relationship ExportColumn::make('gender') the whole relation value are show {"id":2,"name":"Female","created_at":"2024-02-08T04:18:13.000000Z","updated_at":"2024-02-08T04:18:13.000000Z","deleted_at":null} I'm using filament 3.2. Here is my code. What is missing here? UserExporter.php
namespace App\Filament\Exports;

use App\Models\User;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class UserExporter extends Exporter
{
protected static ?string $model = User::class;

public static function getColumns(): array
{
return [
ExportColumn::make('id')
->label('ID'),
ExportColumn::make('name'),
ExportColumn::make('email'),
ExportColumn::make('gender'),
// ExportColumn::make('gender.name'),
];
}
}
namespace App\Filament\Exports;

use App\Models\User;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class UserExporter extends Exporter
{
protected static ?string $model = User::class;

public static function getColumns(): array
{
return [
ExportColumn::make('id')
->label('ID'),
ExportColumn::make('name'),
ExportColumn::make('email'),
ExportColumn::make('gender'),
// ExportColumn::make('gender.name'),
];
}
}
User.php
public function gender(): BelongsTo
{
return $this->belongsTo(gender::class,'gender_id');
}
public function gender(): BelongsTo
{
return $this->belongsTo(gender::class,'gender_id');
}
Gender.php
class Gender extends Model
{
use HasFactory, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];

public function users(): HasMany
{
return $this->hasMany(User::class);
}
}
class Gender extends Model
{
use HasFactory, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];

public function users(): HasMany
{
return $this->hasMany(User::class);
}
}
I'm using filament 3.2. Here is my code. What is missing here?
No description
Solution:
Have you tried updating Filament to latest version? There was a bug in early versions of 3.2 which caused relation data not to show up in export at all.
Jump to solution
3 Replies
Solution
cakan
cakan10mo ago
Have you tried updating Filament to latest version? There was a bug in early versions of 3.2 which caused relation data not to show up in export at all.
toeknee
toeknee10mo ago
Yeah, please run php artisan:about too
ejoi8
ejoi8OP10mo ago
yes, you are correct.. previously mine was v3.2.0. After I updated to v3.2.28, everything work as expected. Thank for point it out
Want results from more Discord servers?
Add your server