Can't export data using ExportAction with Tenant

I'm trying to export data using Filament Export in my Tenant project, but when I try to export data it ends up giving me this error:
Base table or view not found: 1146 Table 'ffrankenphp.company_user' doesn't exist (Connection: mysql, SQL: select `companies`.*, `company_user`.`user_id` as `pivot_user_id`, `company_user`.`company_id` as `pivot_company_id` from `companies` inner join `company_user` on `companies`.`id` = `company_user`.`company_id` where `company_user`.`user_id` = 1 and `companies`.`deleted_at` is null) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ffrankenphp.company_user' doesn't exist (Connection: mysql, SQL: select `companies`.*, `company_user`.`user_id` as `pivot_user_id`, `company_user`.`company_id` as `pivot_company_id` from `companies` inner join `company_user` on `companies`.`id` = `company_user`.`company_id` where `company_user`.`user_id` = 1 and `companies`.`deleted_at` is null) at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:825)
[stacktrace]
Base table or view not found: 1146 Table 'ffrankenphp.company_user' doesn't exist (Connection: mysql, SQL: select `companies`.*, `company_user`.`user_id` as `pivot_user_id`, `company_user`.`company_id` as `pivot_company_id` from `companies` inner join `company_user` on `companies`.`id` = `company_user`.`company_id` where `company_user`.`user_id` = 1 and `companies`.`deleted_at` is null) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ffrankenphp.company_user' doesn't exist (Connection: mysql, SQL: select `companies`.*, `company_user`.`user_id` as `pivot_user_id`, `company_user`.`company_id` as `pivot_company_id` from `companies` inner join `company_user` on `companies`.`id` = `company_user`.`company_id` where `company_user`.`user_id` = 1 and `companies`.`deleted_at` is null) at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:825)
[stacktrace]
The issue is that this relationship is not what I use, the only relationship that exists between these 2 models is that commonarea belongs to companies, but here it tells me that it needs a
company_user
company_user
relationship, I have reviewed a little more and it seems to me that the export migrations when bringing
user_id
user_id
is causing me problems, I made the change to put it as
customer_id
customer_id
since customer is the user who is logged in and from his panel he can download the excel, but even so the problem is not solved... is there any idea that me give to solve this problem?
15 Replies
TranceCode
TranceCodeOP3w ago
I think that the problem is here:
public function up(): void
{
Schema::create('exports', function (Blueprint $table) {
$table->id();
$table->timestamp('completed_at')->nullable();
$table->string('file_disk');
$table->string('file_name')->nullable();
$table->string('exporter');
$table->unsignedInteger('processed_rows')->default(0);
$table->unsignedInteger('total_rows');
$table->unsignedInteger('successful_rows')->default(0);
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('exports', function (Blueprint $table) {
$table->id();
$table->timestamp('completed_at')->nullable();
$table->string('file_disk');
$table->string('file_name')->nullable();
$table->string('exporter');
$table->unsignedInteger('processed_rows')->default(0);
$table->unsignedInteger('total_rows');
$table->unsignedInteger('successful_rows')->default(0);
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
Any idea guys?
TranceCode
TranceCodeOP3w ago
This is the model that i use for export
TranceCode
TranceCodeOP3w ago
and this is my Company Model
TranceCode
TranceCodeOP3w ago
when i change the foreignId to
customer_id
customer_id
or other for test i recieve the same error again!
user_id
user_id
It is as if instead of using the project migration it used the filament package migration at all times
No description
TranceCode
TranceCodeOP3w ago
any idea guys?
toeknee
toeknee3w ago
The problem is on your saving you and saving without the company_id into the export, it looks like you have added company_id to the exports table manually as it doesnt' exist natively. I would recommened removing it or overwrriting the exports model and then apply the company_id by default on save.
TranceCode
TranceCodeOP3w ago
That's true, I added it manually because I need to customize certain things, but the problem is not that, the problem is because it keeps asking me for
user_id
user_id
, as I explained above the image I made the change but it keeps asking me for
user_id
user_id
,
but user_id
but user_id
no longer It is in the migration, therefore the migration that I modified is not taken by filament, I don't know if I understand myself? It seems that it is based more on the migration that comes within the same filament. Even though the
php artisan vendor:publish --tag=filament-actions-migrations
php artisan vendor:publish --tag=filament-actions-migrations
is done and I can modify those migrations, you simply do not take them, which makes me think that no matter how much I modify them to that does not work with user?id it still works with internal migrations. whatever it requires, but it is understood that when we use tenant the User model of that new panel or the model that extends the User Authentication model is another, I don't know if I understand myself?
No description
TranceCode
TranceCodeOP3w ago
Then the problem of the exportAcion and importAcion functionalities are not configured or detailed for tenant and panels in the documentation. I hope you understand my explanation
toeknee
toeknee3w ago
So it expects user_id by default because that’s the creator and that’s the id of the person creating the row not the company since they are tided to users. If you want to make it a company…. Then you need to override the exporter model and define the user id as a company id etc
TranceCode
TranceCodeOP3w ago
Of course it is not the company, out of confusion I put company, but apart from that when I put customer_id it still says that it is missing the user_id, in short Customer is the model of the Company Panel and this extends authenticatable
<?php
namespace App\Models;

use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Sanctum\HasApiTokens;

class Customer extends Authenticatable implements HasTenants
{
use HasApiTokens, HasFactory, Notifiable;

protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

public function companies(): BelongsToMany
{
return $this->belongsToMany(Company::class);
}

public function canAccessTenant(Model $tenant): bool
{
return $this->companies->contains($tenant);
}

public function getTenants(Panel $panel): array|Collection
{
return $this->companies;
}

public function rolesForSelectInCompanyPanel(): BelongsToMany
{
return $this->belongsToMany(Role::class)->where('name', '<>', 'Administrador');
}

public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class);
}

public function permissions(): BelongsToMany
{
return $this->belongsToMany(Permission::class);
}

public function hasRole(string $role): bool
{
return $this->roles->contains('name', $role);
}

public function hasPermission(string $permission): bool
{
return $this->permissions->contains('name', $permission);
}

public function hasAnyRole(array $roles): bool
{
return $this->roles->whereIn('name', $roles)->isNotEmpty();
}
public function hasRoles(): bool
{
return $this->roles->isNotEmpty();
}
}
<?php
namespace App\Models;

use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Sanctum\HasApiTokens;

class Customer extends Authenticatable implements HasTenants
{
use HasApiTokens, HasFactory, Notifiable;

protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

public function companies(): BelongsToMany
{
return $this->belongsToMany(Company::class);
}

public function canAccessTenant(Model $tenant): bool
{
return $this->companies->contains($tenant);
}

public function getTenants(Panel $panel): array|Collection
{
return $this->companies;
}

public function rolesForSelectInCompanyPanel(): BelongsToMany
{
return $this->belongsToMany(Role::class)->where('name', '<>', 'Administrador');
}

public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class);
}

public function permissions(): BelongsToMany
{
return $this->belongsToMany(Permission::class);
}

public function hasRole(string $role): bool
{
return $this->roles->contains('name', $role);
}

public function hasPermission(string $permission): bool
{
return $this->permissions->contains('name', $permission);
}

public function hasAnyRole(array $roles): bool
{
return $this->roles->whereIn('name', $roles)->isNotEmpty();
}
public function hasRoles(): bool
{
return $this->roles->isNotEmpty();
}
}
Well, I will continue testing and analyzing how to solve the problem of both the export and the notifications that I had posted in another problem here, thanks.
toeknee
toeknee3w ago
This isn’t making much sense sorry, I’m not following.
Alnuaimi
Alnuaimi3w ago
If you are using stancl/tenancy You can put this code inside Route/tenant.php inside group just use Filament\Actions\Exports\Http\Controllers\DownloadExport; Route::get('filament/exports/{export}/download', DownloadExport::class) ->name('filament.exports.download') ->middleware(['web', 'auth']);
TranceCode
TranceCodeOP3w ago
Hey bro, thank you for share! I'm not using stancl/tenancy, i'm using the filament configuration and implements HasTenants, i'm trying to use all the configuration of Filament, with which I avoid using third-party packages, in fact I have Laravel Excel installed and configured, but I find that the filament functionality for import and export is much more complete and becomes lighter when using jobs for this task.
awcodes
awcodes3w ago
For what it’s worth, I agree with toeknee. I think you’re overcomplicating by trying to use the company and the user interchangeably which doesn’t make sense in a tenant environment.
TranceCode
TranceCodeOP3w ago
If it is true, perhaps the best option is to pass that data to the table, add customer_id and tenant_id to notifications and nothing more... that way I avoid extra work... which does have me a little confused working with extend of the DatabaseNotification class, to show notifications only to the corresponding tenants and customer accounts. Well, I continue to find out and try options but always maintaining the use of filament instead of third-party packages, thanks bro! Or what would be your recommendation for my case? Do you think it would be a good idea to work with that data in a single table and nothing else?
Want results from more Discord servers?
Add your server