F
Filament8mo ago
ericmp

Automatically generating forms and tables not working?

https://filamentphp.com/docs/3.x/panels/resources/getting-started#automatically-generating-forms-and-tables i do:
php artisan make:filament-resource Transaction --generate
php artisan make:filament-resource Transaction --generate
but i get it all empty, while i expected to have automatically the fields prepared on form and table (and yes, my transaction table has fields, migrations have been ran, etc):
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TransactionResource\Pages;
use App\Filament\Resources\TransactionResource\RelationManagers;
use App\Models\Transaction;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class TransactionResource extends Resource
{
protected static ?string $model = Transaction::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
//
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListTransactions::route('/'),
'create' => Pages\CreateTransaction::route('/create'),
'edit' => Pages\EditTransaction::route('/{record}/edit'),
];
}
}
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TransactionResource\Pages;
use App\Filament\Resources\TransactionResource\RelationManagers;
use App\Models\Transaction;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class TransactionResource extends Resource
{
protected static ?string $model = Transaction::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
//
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListTransactions::route('/'),
'create' => Pages\CreateTransaction::route('/create'),
'edit' => Pages\EditTransaction::route('/{record}/edit'),
];
}
}
Solution:
In your models table name, you have prefixed it with a database prefix: bgpay This is the cause. You should set the mysql connection to have a prefix if the table does within the .env...
Jump to solution
34 Replies
toeknee
toeknee8mo ago
Do you have a model: Transaction and a database table transaction?
ericmp
ericmpOP8mo ago
yes both i already have records actually i already faced this issue months ago and wasnt able to find what was happening
krekas
krekas8mo ago
maybe you need doctrine/dbal package?
ericmp
ericmpOP8mo ago
no, since ive already created other resources and just worked also just checked composer, i have it installed i just dont understand what is going on, since it doesnt work but doesnt tell errors or what could be happening bump
toeknee
toeknee8mo ago
Are you sure there isn't any case issues with naming and they are in the root of the models directory
ericmp
ericmpOP8mo ago
they are in the root of the models directory and wdym about the case issues? the $casts array? @toeknee
toeknee
toeknee8mo ago
case i.e. Transaction::class vs transaction::class
ericmp
ericmpOP8mo ago
hmm, i use the default conventions for naming classes, models, migrations not sure if u mean this well, im gonna do a repo just to test it individually, lets see what happens okay @toeknee i've created this repo - https://github.com/ericmp33/test-filament it has 4 simple commits: 1-install laravel 2-install filament 3-create Transaction model & migration 4-add transactions relationship to User model next step is just doing:
php artisan make:filament-resource Transaction --generate
php artisan make:filament-resource Transaction --generate
in my case i get it all empty im missing something? (yes, ive ran the migrations) if u want u can clone it and test it (if u want 🙌 )
toeknee
toeknee8mo ago
Wiull do Quick Q have you tested on Laravel 10?
ericmp
ericmpOP8mo ago
noup, only in L11 but weeks ago this same issue happened to me in a L10 app, where i wasnt able to create a filament resource with --generate flag
toeknee
toeknee8mo ago
You have caused it
ericmp
ericmpOP8mo ago
howwww
Solution
toeknee
toeknee8mo ago
In your models table name, you have prefixed it with a database prefix: bgpay This is the cause. You should set the mysql connection to have a prefix if the table does within the .env
ericmp
ericmpOP8mo ago
oh my bad, that wasnt suposed to be there 🤦 let me review it & ill try again
toeknee
toeknee8mo ago
Nope as it doesn't exist in the migration. so remove all prefixes and it'll generate just fine.
awcodes
awcodes8mo ago
you might need to manually install dbal too, if you haven't. L11 completely removed its dependancy on the package. https://laravel.com/docs/11.x/upgrade#doctrine-dbal-removal
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
toeknee
toeknee8mo ago
No it's part of the install already 🙂
awcodes
awcodes8mo ago
👍 just pointing it out.
ericmp
ericmpOP8mo ago
okay now works
ericmp
ericmpOP8mo ago
so doesnt support multiple database connections? or how? because this repo test comes from an app with multiple databases sharing a common db storing the users that is why i had those prefixes cuz i have multiple db connections but for the test i just setup one, without the others
toeknee
toeknee8mo ago
IT will do yes, but not as a table name You would need to add the prefix to the database connection with a prefix .env Just create different connection types for each connection mysql_1 mysql_2 mysql_3 all with their own .env constants. I do it for multiple databases across multiple database types.
ericmp
ericmpOP8mo ago
but i already have the connections in database laravel config file & in .env
toeknee
toeknee8mo ago
Then set the prefix in there?
ericmp
ericmpOP8mo ago
example:
# common_database
COMMON_DB_HOST=127.0.0.1
COMMON_DB_PORT=3306
COMMON_DB_DATABASE=id
COMMON_DB_USERNAME=root
COMMON_DB_PASSWORD=

# forms_database
FORMS_DB_HOST=127.0.0.1
FORMS_DB_PORT=3306
FORMS_DB_DATABASE=forms
FORMS_DB_USERNAME=root
FORMS_DB_PASSWORD=
# common_database
COMMON_DB_HOST=127.0.0.1
COMMON_DB_PORT=3306
COMMON_DB_DATABASE=id
COMMON_DB_USERNAME=root
COMMON_DB_PASSWORD=

# forms_database
FORMS_DB_HOST=127.0.0.1
FORMS_DB_PORT=3306
FORMS_DB_DATABASE=forms
FORMS_DB_USERNAME=root
FORMS_DB_PASSWORD=
like this is ok? not sure wdym by prefix
toeknee
toeknee8mo ago
Yes, it is. but you don't have the prefix if you are using prefixes
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
So there is no env for prefix, so just add it if they have prefixs
ericmp
ericmpOP8mo ago
ohhh so u mean to add in there: 'prefix' => 'bgpay' and in the forms one: 'prefix' => 'forms' etc the file is config/database.php right? i tried it like this but still doesnt generate it, im doing something wrong? but im not using prefixes, doing this -> protected $table = 'bgpay.transactions'; i was just defining that the database from where the table transactions belongs to, no? a prefix would be protected $table = 'bgpay_transactions'; but is a dot, with a dot is still a prefix?
toeknee
toeknee8mo ago
So if no prefixes no need to use them and no need for a dot. The bgpay you just need to set the MySQL connection. mysql, mysql_bgpay Then clone MySQL in database.php config snd name it mysql_bgpay?
ericmp
ericmpOP8mo ago
i do need to define the $table attribute like that, otherwise i'm unable to do relationships between models of diferent dbs in each model i define the $connection & the $table both needed in this case otherwise, when calling relations it tries to search the table on same database, but the related model may be in other database table, so that is why i need the $table attribute with the database name and then the dot and then the table name with this setup, i guess filament cant figure out how to generate the resources or idk how to tell filament to do so in the demo project i created, yeah it works cuz i dont have this multiple db setup
toeknee
toeknee8mo ago
So if you define connection and table you don't need to dot prefix the table? You just use natively relationships between the models. Else if you were happy with how it was working less the generate, just do it that way and don't use generate.
ericmp
ericmpOP8mo ago
okay then yeah, i dont need prefixes. not sure why u suggested it but might be useful for other cases, not sure
toeknee
toeknee8mo ago
Because you had a dot notation in your code as if it was a prefix
ericmp
ericmpOP8mo ago
where? yeah, i think ill do this for now and generate it by myself yeah no prob 🦾
toeknee
toeknee8mo ago
in the table name originally
ericmp
ericmpOP8mo ago
hmmm i dont think so, i was just using protected $table = 'bgpay.transactions'; with the dot well nvm dont worry thanks for the help
Want results from more Discord servers?
Add your server