Combining 2 fields in one table's column

in my company table i have this code

Tables\Columns\TextColumn::make('customers.name')
->sortable(),

Tables\Columns\TextColumn::make('customers.name')
->sortable(),
this show only the name, the relation between company and customer is

public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class, 'repairshop_company_customer');
}

public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class, 'repairshop_company_customer');
}
i need to concat customer.name and customers.last_name tried different approach but without success
20 Replies
einnlleinhatt_
einnlleinhatt_13mo ago
You can use formatStateUsing
Tables\Columns\TextColumn::make('user.first_name')
->label('Customer Name')
->formatStateUsing(function ($state, Order $order) {
return $order->user->first_name . ' ' . $order->user->last_name;
}),
Tables\Columns\TextColumn::make('user.first_name')
->label('Customer Name')
->formatStateUsing(function ($state, Order $order) {
return $order->user->first_name . ' ' . $order->user->last_name;
}),
Soundmit
SoundmitOP13mo ago
i found this examples but doesn't works..
einnlleinhatt_
einnlleinhatt_13mo ago
Check your relationship maybe
AmauryCid
AmauryCid13mo ago
Another way:
Tables\Columns\TextColumn::make('consumers.name')
->state(function (Consumer $record): string {
return $record->first_name . ' ' . $record->last_name;
})
Tables\Columns\TextColumn::make('consumers.name')
->state(function (Consumer $record): string {
return $record->first_name . ' ' . $record->last_name;
})
Soundmit
SoundmitOP13mo ago
in my customer model

public function company(): BelongsToMany
{
return $this->belongsToMany(Company::class, 'repairshop_company_customer');
}

public function company(): BelongsToMany
{
return $this->belongsToMany(Company::class, 'repairshop_company_customer');
}
in my company model

public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class, 'repairshop_company_customer');
}

public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class, 'repairshop_company_customer');
}
in my companyResource table definition this show the name of the customers (one or more)

Tables\Columns\TextColumn::make('customers.name')
->sortable(),

Tables\Columns\TextColumn::make('customers.name')
->sortable(),
this one show company name (not the customer name)

Tables\Columns\TextColumn::make('customers.name')
->label('Customer Name')
->formatStateUsing(function ($state, Company $company) {
return $company->name;
}),

Tables\Columns\TextColumn::make('customers.name')
->label('Customer Name')
->formatStateUsing(function ($state, Company $company) {
return $company->name;
}),
this show nothing

Tables\Columns\TextColumn::make('customers.name')
->label('Customer Name')
->formatStateUsing(function ($state, Customer $company) {
return $company->name;
}),

Tables\Columns\TextColumn::make('customers.name')
->label('Customer Name')
->formatStateUsing(function ($state, Customer $company) {
return $company->name;
}),
this show company name

Tables\Columns\TextColumn::make('customers.name')
->state(function (Company $record): string {
return $record->name . ' ' . $record->last_name;
}),

Tables\Columns\TextColumn::make('customers.name')
->state(function (Company $record): string {
return $record->name . ' ' . $record->last_name;
}),
this gives me this error

Tables\Columns\TextColumn::make('customers.name')
->state(function (Customer $record): string {
return $record->name . ' ' . $record->last_name;
}),

Tables\Columns\TextColumn::make('customers.name')
->state(function (Customer $record): string {
return $record->name . ' ' . $record->last_name;
}),

App\Filament\Resources\CompanyResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Customer, App\Models\Company given, called in /var/www/html/vendor/filament/support/src/Concerns/Evaluates

App\Filament\Resources\CompanyResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Customer, App\Models\Company given, called in /var/www/html/vendor/filament/support/src/Concerns/Evaluates
the pivot table in the db

Schema::create('repairshop_company_customer', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id');
$table->foreignId('customer_id');
});

Schema::create('repairshop_company_customer', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id');
$table->foreignId('customer_id');
});
einnlleinhatt_
einnlleinhatt_13mo ago
Well you put consumer instead of customer.
Soundmit
SoundmitOP13mo ago
yes yes, a typo but the errror remain
einnlleinhatt_
einnlleinhatt_13mo ago
Don't you need pass the company id and user id in the customers()?
Soundmit
SoundmitOP13mo ago
idk, all the examples above are from a search on google if i use this

Tables\Columns\TextColumn::make('customers.name')
->state(function (Customer $record): string {
return $record->name . ' ' . $record->last_name;
}),

Tables\Columns\TextColumn::make('customers.name')
->state(function (Customer $record): string {
return $record->name . ' ' . $record->last_name;
}),
the error is strange

App\Filament\Resources\CompanyResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Customer, App\Models\Company given, called in

App\Filament\Resources\CompanyResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Customer, App\Models\Company given, called in
i'm using App\Models\Customer not App\Models\Company
einnlleinhatt_
einnlleinhatt_13mo ago
You use Customer modal but the customers relationship is inside company modal.
Soundmit
SoundmitOP13mo ago
yes and i'm in the CompanyResource
einnlleinhatt_
einnlleinhatt_13mo ago
Because you don't have customers inside your customrer model.
einnlleinhatt_
einnlleinhatt_13mo ago
No description
lazydog
lazydog13mo ago
Alternative solution to make your life easier, add this in your migration
$table->string('field_name')->virtualAs('concat(name, \' \', last_name)');
$table->string('field_name')->virtualAs('concat(name, \' \', last_name)');
and in your table list just call the
field_name
field_name
einnlleinhatt_
einnlleinhatt_13mo ago
Maybe show your database schema
Soundmit
SoundmitOP13mo ago
Perhaps I am not understanding. In the display of the table with all the companies, I need to show the customer associated with the company. The customer model is present in the company, and vice versa. The relationship is present in both models. this is the pivot table

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('repairshop_company_customer', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id');
$table->foreignId('customer_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('repairshop_company_customer');
}
};

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('repairshop_company_customer', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id');
$table->foreignId('customer_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('repairshop_company_customer');
}
};
companies and customers table are regular tables Yes, the virtualAs is not a bad solution, but if there is an alternative, I would like to know it. done with virtual field
einnlleinhatt_
einnlleinhatt_13mo ago
I mean the customer and company table too
Soundmit
SoundmitOP13mo ago
customer
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('repairshop_customers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('last_name');
$table->string('field_name')->virtualAs('concat(name, \' \', last_name)');
$table->string('slug')->unique();

$table->foreignId('created_by');

#ENUM
$table->string('customer_type');

$table->string('email')->unique();
$table->string('mobile')->nullable();
$table->string('phone')->nullable();
$table->json('contact_method')->nullable();

$table->string('address')->nullable();
$table->string('city')->nullable();;
$table->string('province')->nullable();
$table->string('zip_code', 10)->nullable();

$table->text('note')->nullable();

$table->string('cf')->nullable();
$table->string('born_in')->nullable();
$table->dateTime('born_date')->nullable();

$table->string('documents')->nullable();

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('repairshop_customers');
}
};
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('repairshop_customers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('last_name');
$table->string('field_name')->virtualAs('concat(name, \' \', last_name)');
$table->string('slug')->unique();

$table->foreignId('created_by');

#ENUM
$table->string('customer_type');

$table->string('email')->unique();
$table->string('mobile')->nullable();
$table->string('phone')->nullable();
$table->json('contact_method')->nullable();

$table->string('address')->nullable();
$table->string('city')->nullable();;
$table->string('province')->nullable();
$table->string('zip_code', 10)->nullable();

$table->text('note')->nullable();

$table->string('cf')->nullable();
$table->string('born_in')->nullable();
$table->dateTime('born_date')->nullable();

$table->string('documents')->nullable();

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('repairshop_customers');
}
};
companies

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('repairshop_companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();

$table->string('address')->nullable();
$table->string('city')->nullable();;
$table->string('province')->nullable();
$table->string('zip_code', 10)->nullable();

$table->string('email')->unique();
$table->string('pec')->nullable();
$table->string('mobile')->nullable();
$table->string('phone')->nullable();

$table->string('website')->nullable();
$table->string('vat')->nullable();
$table->string('cf')->nullable();
$table->string('sdi')->nullable();

$table->text('note')->nullable();
$table->string('company_logo')->nullable();
$table->foreignId('created_by');

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('repairshop_companies');
}
};

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('repairshop_companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();

$table->string('address')->nullable();
$table->string('city')->nullable();;
$table->string('province')->nullable();
$table->string('zip_code', 10)->nullable();

$table->string('email')->unique();
$table->string('pec')->nullable();
$table->string('mobile')->nullable();
$table->string('phone')->nullable();

$table->string('website')->nullable();
$table->string('vat')->nullable();
$table->string('cf')->nullable();
$table->string('sdi')->nullable();

$table->text('note')->nullable();
$table->string('company_logo')->nullable();
$table->foreignId('created_by');

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('repairshop_companies');
}
};
einnlleinhatt_
einnlleinhatt_13mo ago
Id you change the Customer model into company still not work ?
Soundmit
SoundmitOP13mo ago
I will look into tonight, thanks for the help
Want results from more Discord servers?
Add your server