Relationship Does Not Exists
I'm struggling to make two tables connect. Seems straight forward:
1. Create your two tables and migrate them. Make sure your migration in one of the tables is connected to the other: $table->foreignId('owner_id')->constrained()->cascasdOnDelete(); 2. Create models for both tables. Fill in the 'protected $fillable' and make sure to create the relationship:
3. Create your Filament resource and call it from there:
1. Create your two tables and migrate them. Make sure your migration in one of the tables is connected to the other: $table->foreignId('owner_id')->constrained()->cascasdOnDelete(); 2. Create models for both tables. Fill in the 'protected $fillable' and make sure to create the relationship:
public function alhusers(): HasOne {
return $this->hasOne('ahlusers::class');
}
3. Create your Filament resource and call it from there:
TextInput::make('users')
->relationship('ahlusers', 'abbr'),
And here is the error I'm receiving:
Method Filament\Forms\Components\TextInput::relationship does not exist.
Yes, I can appreciate this is exactly what it means yet I'm not sure how to troubleshoot this any further. I checked for typos and looked through Google for solutions however I can't find a solution as they are in Laravel-specific context and years old - hence my post here. Thanks in advance.11 Replies
Hi, there is a typo
alhusers and ahlusers
That didn't fix it and I realized that the table is lowercase and the class/filename (first letter) is upper. Is this even relevant?
It did not solve it. I do see the class and filename (first letter) is uppercase and other areas (i.e. table within the db) is all lowercase. Is this relevant/connected to my issue perhaps?
The error is clear. TextInput form component doesn’t have a relationship method. You would need to wrap it in a Group which does have a relationship method.
Or a Section or one of the other layout components.
Which makes sense because typically, a relationship wouldn’t exist for a single form value like a text field.
I'm obviously missing something so I'll start fresh, step by step.
Step 1. Create the migration
This is simple enough and starting to make sense. I can see I can play with navigation settings, icons and other cool things. More importantly, this page dictates how my table and form pages look like. And here in lies some of the confusion. If, for example, I'm looking at the owners form or table - does there not have to be a pre-existing relationship within the Owners model to determine how the Ahlusers table is displayed/updated/etc. here? Below is a snippet from my OwnersResource form
Schema::create('owners', function (Blueprint $table) {
$table->id();
$table->string('first')->nullable();
$table->string('last')->nullable();
$table->string('email')->nullable();
...
$table->timestamps();
});
Schema::create('ahlusers', function (Blueprint $table) {
$table->id();
$table->string('first')->nullable();
$table->string('last')->nullable();
$table->string('email')->nullable();
...
$table->timestamps();
});
In the above instance (two separate files) I understand that I am creating two tables and wish to connect them each other. Great. Done. Looks good within the db, and the first magic of this happens with:
$table->foreignId('owners_id')->constrained()->cascasdOnDelete();
This code connects both tables together using the owners_id (ahlusers) on the id (owners) table respectively. Or atleast that what I think is going on. And that leads us to step 2.
Step 2. Create the Models associated with the tables you just created.
namespace App\Models;
class Owners extends Model
protected $fillable = [
'id',
'first',
'last',
'email',
...
];
class Ahlusers extends Model
protected $fillable = [
'id',
'owner_id',
'name',
'abbr',
'division',
'logo',
...
];
I've noted that all the fields in the migration stage (previous) obviously match up with the models. Great. What's next?
Step 3. Create the Resources, right?This is simple enough and starting to make sense. I can see I can play with navigation settings, icons and other cool things. More importantly, this page dictates how my table and form pages look like. And here in lies some of the confusion. If, for example, I'm looking at the owners form or table - does there not have to be a pre-existing relationship within the Owners model to determine how the Ahlusers table is displayed/updated/etc. here? Below is a snippet from my OwnersResource form
return $form
->schema([
TextInput::make('id')->required(),
???????TEXT/SETTINGS?::make('')
->label('ahlusers abbr Owners form')
->relationship('ahlusers', 'abbr'),
HOW DO I PLACE THE MATCHING ID (or whatever) OF THE Ahlusers table HERE?
TextInput::make('first'),
TextInput::make('last'),
TextInput::make('email'),
]);
In other words: How can I connect and/or place everything from both tables within the database in 1 form that I can update?
I followed this tutorial which led me to the Select dropdown option as you mentioned (hxxps://www.youtube.com/watch?v=c_hL4wKYfHY&list=PLqDySLfPKRn6fgrrdg4_SmsSxWzVlUQJo&index=8). He does a great example of showing how to connect categories to posts and it's what I've been working off of. However in my context I don't wish for a select or dropdown menu to select from. This is because is because I don't have multiple posts and one category. I have 1 id from 1 table and 1 owner_id from another that will never change and always match. Would it not be something like the following?
->options(ahlusers::all()->pluck('name', 'id'))
->relationship('ahlusers', 'abbr'),
If not a text field and not a select dropdown then what type of relationship method do I place on my Owners Resource page? Am I correct in thinking it should be something to this effect?
public function ahlusers(): HasOne { return $this->hasOne('Ahlusers::class'); }
While I feel like I'm on the right track - I'm missing something fundamental here obviously. Hopefully I've articulated myself in a manner that some users can gain something beneficial at this point.
all of the layout components support relationships
Thank you. I've seemed to have graduated to a new issue. Still too new to know exactly what to correct or where to look.
I'll start a new ticket somewhere else and report back here when this is solved.
Solution
Your relationship is not defined properly. Don’t quote alhusers::class
Thank you. It was the quote around the class. solved. I hope this post assists someone else in the future and I appreciate everyone's assistance on this.