F
Filament12mo ago
MikeG

Tenancy for laravel & File upload component

Hello, I'm running into an issue that I can't seem to figure out why it's happening. I use Tenancy for Laravel in my application for multi-tenant capabilities. I have a resource, with a form that contains a file upload. But for some reason, when trying to upload a file, I get the following error: base table or view not found: 1146 Table 'main_db.users' doesn't exist I'm confused about why it's trying to access the user's table, and even more so why it's using the central database main_db rather than the tenant's database. Does anyone know why this is happening?
Solution:
Ah @Dennis Koch so I did find it, livewire configures it on temp file uploads: in livewire.php config file...
Jump to solution
26 Replies
Dennis Koch
Dennis Koch12mo ago
Did you set the tenant middlewares for Livewire requests?
MikeG
MikeGOP12mo ago
I did 😦
Dennis Koch
Dennis Koch12mo ago
Hm, weird. I guess the Filament middleware is set, otherwise you would have had other issues.
MikeG
MikeGOP12mo ago
You mean having the tenancy middleware on the panel? Also, any idea why it would be trying to read the Users model upon fileupload? Im seriously struggeling with debugging this one 😂
Dennis Koch
Dennis Koch12mo ago
Anything special with your FileUpload?
Also, any idea why it would be trying to read the Users model upon fileupload? Im seriously struggeling with debugging this one 😂
Authentication
You mean having the tenancy middleware on the panel?
Yes.
MikeG
MikeGOP12mo ago
Not really..
Forms\Components\FileUpload::make('file')
->label('Document')
->helperText('This document will be uploaded in to the knowledgebase')
->storeFiles(false)
->required(),
Forms\Components\FileUpload::make('file')
->label('Document')
->helperText('This document will be uploaded in to the knowledgebase')
->storeFiles(false)
->required(),
Dennis Koch
Dennis Koch12mo ago
Can you share the stack trace via Flare?
MikeG
MikeGOP12mo ago
I do have a custom create action, which implements the using() method but that would only be hit post creation. This is happening upon completing of the upload already
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->using(function(array $data) {
if(Auth::user()->viewing_voiceflow_project_id != null) {
$vfProject = VoiceflowProject::find(Auth::user()->viewing_voiceflow_project_id);

$vfKnowledgeBaseApi = new KnowledgeBaseApi($vfProject->dialog_api_token);

dd('test');
$storeDocument = new StoreDocument($data['file']);

$response = $vfKnowledgeBaseApi->send($storeDocument);

ray($response);
return true;
}

return false;
}),
];
}
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->using(function(array $data) {
if(Auth::user()->viewing_voiceflow_project_id != null) {
$vfProject = VoiceflowProject::find(Auth::user()->viewing_voiceflow_project_id);

$vfKnowledgeBaseApi = new KnowledgeBaseApi($vfProject->dialog_api_token);

dd('test');
$storeDocument = new StoreDocument($data['file']);

$response = $vfKnowledgeBaseApi->send($storeDocument);

ray($response);
return true;
}

return false;
}),
];
}
MikeG
MikeGOP12mo ago
Flare
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sapphireportal.users' doesn't exist (Connection: central, SQL: select * from users where id = 1 limit 1) - The error occurred at https://mike.sapphireportal.test/livewire/upload-file?expires=1703762991&signature=367d29b3b740cecff1d062b4d16a1b11dd68ca3dad799cd0700e2a6640d7de52
Dennis Koch
Dennis Koch12mo ago
Can you maybe try a new super simple form with just the upload? Just to make sure
MikeG
MikeGOP12mo ago
sure
Dennis Koch
Dennis Koch12mo ago
Do you have a throttle in your web middleware group?
MikeG
MikeGOP12mo ago
So i've created a completely new resource + model, and only included a fileupload component, getting the same error
Dennis Koch
Dennis Koch12mo ago
You don't have universal as a middleware as in the docs
MikeG
MikeGOP12mo ago
No throttling set on web Ye i just removed it for testing, it was there before, though the universal middleware is an empty array in Kernel so doesnt seem to really be doing anything I will re-add it just incase though
Dennis Koch
Dennis Koch12mo ago
Tenancy for Laravel
Universal routes | Tenancy for Laravel
Universal routes | Tenancy for Laravel
Dennis Koch
Dennis Koch12mo ago
But yeah. Universal routes shouldn't be the issue here. Looking at your stack trace, the error comes from the ThrottleRequests middleware
MikeG
MikeGOP12mo ago
Hmm, interestingly I do see the throttle middleware applied in that flare debug info, so it must be called somewhere somehow
Dennis Koch
Dennis Koch12mo ago
Can you share your web middleware group?
MikeG
MikeGOP12mo ago
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
And:
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
Also, the panel middelware itself:
->middleware([
InitializeTenancyByDomainOrSubdomain::class,
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
RegisterSegmentGlobalsMiddleware::class,
RegisterNavigateToSegmentMiddleware::class
])
->middleware([
InitializeTenancyByDomainOrSubdomain::class,
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
RegisterSegmentGlobalsMiddleware::class,
RegisterNavigateToSegmentMiddleware::class
])
What made you conclude that btw? It's weird that the throttle middleware is being included in the request, I cant seem to find anywhere that is being set for this route :/ Trying to figure that out to disable it and test
Solution
MikeG
MikeG12mo ago
Ah @Dennis Koch so I did find it, livewire configures it on temp file uploads: in livewire.php config file
MikeG
MikeGOP12mo ago
After changing that middleware to not include it, it seems to do the trick 🙂 Now getting a 422 error on file upload size, so I think that fixed the issue I was having 😄 If I may ask, just for my own learning, how did you end up concluding it was the throttling from that stack trace? I did not conclude that at all. Changed max size, upload returning success now, ty so much @Dennis Koch, you've saved me a lot of time ❤️
Dennis Koch
Dennis Koch12mo ago
Nice. Glad to help
DrByte
DrByte12mo ago
I'm guessing it was because it was listed in the Vendor Frames and proceeded into the file further than just hitting a return($next). It's mentioned twice, which makes it stand out a little.
Dennis Koch
Dennis Koch12mo ago
Sorry, I missed your question. But Dr. Byte already answered. I opened the collapsed steps and went through them until there was something helpful 😅
MikeG
MikeGOP12mo ago
Makes sense, ty for the info 😄
Want results from more Discord servers?
Add your server