BelongsToMany relationship in Repeater

Hey, I've got two models - Ad & Location. The Location model uses a custom primary key $table->string('ekatte')->primary();. Those are both relationships:
// Location
public function ads(): BelongsToMany
{
return $this->belongsToMany(Ad::class);
}

// Ad
public function locations(): BelongsToMany
{
return $this->belongsToMany(Location::class);
}
// Location
public function ads(): BelongsToMany
{
return $this->belongsToMany(Ad::class);
}

// Ad
public function locations(): BelongsToMany
{
return $this->belongsToMany(Location::class);
}
When trying to edit an Ad and insert more records using a Repeater with a relationship set, it fails with an error that the property location_ekatte must be added to the $fillable array in the Location model. That property doesn't even exist there... You can check the attached file. I really appreciate any help you can provide.
10 Replies
Georgi Arnaudov
Georgi ArnaudovOP2y ago
Lol, im such a dummy, thanks @Dan Harrin Sorry to bother you again, but I slept on the issue and figured that HasMany may not be appropriate. The reason is that I have predefined Locations and want to attach multiple to the Ad. For me, that would be done with a pivot table holding the ad_id and the location_ekatte. I think the problem is in the naming of the fields in the pivot table.
Dan Harrin
Dan Harrin2y ago
the hasmany is to the pivot table you can still use a belongstomany for the main relationship you just need a hasmany for the repeater so we know which table to write data to
Georgi Arnaudov
Georgi ArnaudovOP2y ago
Right after I posted my second answer tried with a HasMany as you just suggested. Works like a charm! Thanks for you time!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Georgi Arnaudov
Georgi ArnaudovOP2y ago
@n3b0r Sure, you just have to add a HasMany to a model that represents the pivot.
public function locations(): HasMany
{
return $this->hasMany(AdLocation::class);
}
public function locations(): HasMany
{
return $this->hasMany(AdLocation::class);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Georgi Arnaudov
Georgi ArnaudovOP2y ago
Thats the repeater
Repeater::make('locations')
->visible(fn ($get) => $get('type') === 'offer')
->relationship()
->label('Междинни спирки')
->helperText('')
->minItems(1)
->defaultItems(1)
->schema([
...
]),
Repeater::make('locations')
->visible(fn ($get) => $get('type') === 'offer')
->relationship()
->label('Междинни спирки')
->helperText('')
->minItems(1)
->defaultItems(1)
->schema([
...
]),
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Georgi Arnaudov
Georgi ArnaudovOP2y ago
Glad you found the issue! Keep it up mate.
Want results from more Discord servers?
Add your server