Update repeater

https://flareapp.io/share/VmeWKgz7
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause'
update
`artist_lead`
SET
`artist_id` = 16,
`artist_lead`.`updated_at` = 2023 -10 -27 13: 50: 46
WHERE
`` = 15
AND `` = 15
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause'
update
`artist_lead`
SET
`artist_id` = 16,
`artist_lead`.`updated_at` = 2023 -10 -27 13: 50: 46
WHERE
`` = 15
AND `` = 15
Repeater::make('leadArtists')
->required()
->relationship()
->label(__('lead-resource.repeater.field.artists'))
->schema([
Forms\Components\Select::make('artist_id')
->searchable()
->relationship('artist', 'name')
->getSearchResultsUsing(fn(string $search): array => Artist::where('name', 'like', "{$search}%")
->orWhere('first_name', 'like', "{$search}%")
->orWhere('zip_code', 'like', "{$search}%")
->select('id', 'name', 'first_name', 'zip_code')
->limit(50)
->get()
->mapWithKeys(fn($artist) => [$artist->id => "{$artist->first_name} {$artist->name} - {$artist->zip_code}"])
->toArray())
->getOptionLabelUsing(function ($value): ?string {
$artist = Artist::find($value);
return "{$artist->first_name} {$artist->name} - {$artist->zip_code}";
})
->label(__('lead-resource.repeater.field.name'))
->columnSpan(2),
Repeater::make('leadArtists')
->required()
->relationship()
->label(__('lead-resource.repeater.field.artists'))
->schema([
Forms\Components\Select::make('artist_id')
->searchable()
->relationship('artist', 'name')
->getSearchResultsUsing(fn(string $search): array => Artist::where('name', 'like', "{$search}%")
->orWhere('first_name', 'like', "{$search}%")
->orWhere('zip_code', 'like', "{$search}%")
->select('id', 'name', 'first_name', 'zip_code')
->limit(50)
->get()
->mapWithKeys(fn($artist) => [$artist->id => "{$artist->first_name} {$artist->name} - {$artist->zip_code}"])
->toArray())
->getOptionLabelUsing(function ($value): ?string {
$artist = Artist::find($value);
return "{$artist->first_name} {$artist->name} - {$artist->zip_code}";
})
->label(__('lead-resource.repeater.field.name'))
->columnSpan(2),
why my fields are empty ?
Flare
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (Connection: mysql, SQL: update artist_lead set artist_id = 16, artist_lead.updated_at = 2023-10-27 14:48:13 where = 15 and = 15) - The error occurred at http://localhost/admin/leads/15/edit
Solution:
Just @ me if you need more help. This stuff can be confusing.
Jump to solution
14 Replies
ConnorHowell
ConnorHowell9mo ago
Is leadArtists a belongsToMany relationship? If so are you using a pivot model? And if you are, your pivot model needs a primary key; which judging by the error I assume it doesn't have?
Becker Maxime
Becker Maxime9mo ago
public function leadArtists(): HasMany
{
return $this->hasMany(ArtistLead::class);
}
public function leadArtists(): HasMany
{
return $this->hasMany(ArtistLead::class);
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;

class ArtistLead extends Pivot
{
use HasFactory;

protected $fillable = [
'lead_id',
'artist_id',
'accept',
'refuse',
];

public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}

public function lead(): BelongsTo
{
return $this->belongsTo(Lead::class);
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;

class ArtistLead extends Pivot
{
use HasFactory;

protected $fillable = [
'lead_id',
'artist_id',
'accept',
'refuse',
];

public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}

public function lead(): BelongsTo
{
return $this->belongsTo(Lead::class);
}
}
so that's not the case, I don't understand why it makes a where twice the identifier of my lead.
cheesegrits
cheesegrits9mo ago
Does your artist_lead table have a primary key, like 'id'? The code you quoted won't show that. Maybe paste the migration.
Becker Maxime
Becker Maxime9mo ago
public function up(): void
{
Schema::create('artist_lead', function (Blueprint $table) {
$table->unsignedBigInteger('lead_id');
$table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
$table->unsignedBigInteger('artist_id');
$table->foreign('artist_id')->references('id')->on('artists')->onDelete('cascade');
$table->boolean('accept')->default(0);
$table->boolean('refuse')->default(0);
$table->primary(['lead_id', 'artist_id']);
$table->timestamps();
});
}
public function up(): void
{
Schema::create('artist_lead', function (Blueprint $table) {
$table->unsignedBigInteger('lead_id');
$table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
$table->unsignedBigInteger('artist_id');
$table->foreign('artist_id')->references('id')->on('artists')->onDelete('cascade');
$table->boolean('accept')->default(0);
$table->boolean('refuse')->default(0);
$table->primary(['lead_id', 'artist_id']);
$table->timestamps();
});
}
I have two tables leads and artists and a pivot table artist_lead
ConnorHowell
ConnorHowell9mo ago
Yeah so you’re missing a primary key on artist_lead, refer to the link I sent it’s required for the repeater to work “Please ensure that your pivot model has a primary key column, like id, to allow Filament to keep track of which repeater items have been created, updated and deleted.”
Becker Maxime
Becker Maxime9mo ago
my pivot model has two primary keys, artist_id and lead_id, I don't understand why I should add a primary key. I need to add $table->id() in my migration ?
cheesegrits
cheesegrits9mo ago
Usually a pivot table doesn't need a singular primary key like 'id', it uses a composite key, like you did. Because usually you aren't directly working on the pivot table itself, you are working with the target table on the other "side" of the pivot, with BelongsToMany relationships. But when you need to work on the actual pivot itself, where you create a model for the pivot, you do need a singular primary key. Laravel (and hence Filament) doesn't natively support composite keys for models. So the migration would need to look more like this:
public function up(): void
{
Schema::create('artist_lead', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('lead_id');
$table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
$table->unsignedBigInteger('artist_id');
$table->foreign('artist_id')->references('id')->on('artists')->onDelete('cascade');
$table->boolean('accept')->default(0);
$table->boolean('refuse')->default(0);
$table->unique(['lead_id', 'artist_id']);
$table->timestamps();
});
}
public function up(): void
{
Schema::create('artist_lead', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('lead_id');
$table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
$table->unsignedBigInteger('artist_id');
$table->foreign('artist_id')->references('id')->on('artists')->onDelete('cascade');
$table->boolean('accept')->default(0);
$table->boolean('refuse')->default(0);
$table->unique(['lead_id', 'artist_id']);
$table->timestamps();
});
}
The unique() isn't really needed, but you can include it if you want to enforce referential integrity.
Becker Maxime
Becker Maxime9mo ago
Ok, but I use hasMany to make my repeater work, do I have to sing again for belongsToMany()?
cheesegrits
cheesegrits9mo ago
The takeaway here is that when you need to create a model for the pivot and work directly on it, rather than simply using a pivot as part of relationships and only working on the models either "side" of the pivot, then you need a singular primary key. This is a limitation of Laravel. Your BelongsToMany relationships should still work, as they only care about the two foreign keys.
Becker Maxime
Becker Maxime9mo ago
Ok thanks for your help, I'll try that on Monday. Thanks
cheesegrits
cheesegrits9mo ago
N/p
Solution
cheesegrits
cheesegrits9mo ago
Just @ me if you need more help. This stuff can be confusing.
Becker Maxime
Becker Maxime9mo ago
Thank you very much for your time. I hope I won't have to call you back. @Hugh Messenger Thank you very much. it works, I didn't know laravel didn't handle composite keys, thank you very much. Bisous