relationship
can i ask some help have 2 tables users and orders i need to iinsert name data from users to orders data tables . May I know how can i do that?
16 Replies
TextColumn::make('user.name')
assuming that you have a relation user() on Order model.
i see i think i am missing that part to add relation on order model
may i know how i will do that
to handle you can override function handleRecordCreation(array $data) on create pages, and write your custom logic
do we have any sample on filament docs?
i have order resource and order model
order resource were i display data from database
I am not sure what you want exactly.. so here is an example (not tested)
i see will this need to run migratio or no need? becasue i don't want to modify the database table
do you want to insert data to user table and order table from order resource?
i just want to pull/fetch data from users table and display on order resource
just want to populate the table from existing data
on order model add relation to user then on your OrderResource > table function add TextColumn::make('user.column')
you can read here
https://filamentphp.com/docs/2.x/tables/columns/relationships#displaying-data-from-relationships
Filament
Column relationships - Columns - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
i will check it thank you
is this correct?
order resource:
return $table
->columns([
Tables\Columns\TextColumn::make('user.name')->label('User Name')
order model public function user(): BelongsTo { return $this->belongsTo(User::class); }
protected $table = 'outbound_orders'; public $timestamps = true; protected $fillable = [ 'id', 'user_id', 'order_number',
user table public function up(): void { Schema::create('users', function (Blueprint $table) { $table->id(); $table->foreignId('user_id'); $table->string('name'); i want to grab this $table->string('name'); to dsiplay on order table
order model public function user(): BelongsTo { return $this->belongsTo(User::class); }
protected $table = 'outbound_orders'; public $timestamps = true; protected $fillable = [ 'id', 'user_id', 'order_number',
user table public function up(): void { Schema::create('users', function (Blueprint $table) { $table->id(); $table->foreignId('user_id'); $table->string('name'); i want to grab this $table->string('name'); to dsiplay on order table
yes
but i got this error
App\Models\CustomerOrders::user(): Return value must be of type App\Models\BelongsTo, Illuminate\Database\Eloquent\Relations\BelongsTo returned
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
add this on your model
use Illuminate\Database\Eloquent\Relations\BelongsTo;
or delete the return type on function
public function user()
{
return $this->belongsTo(User::class);
}
yes thank you i forgot to add that
btw i add another relationship but its not working
is this correct if i want to add more
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function orderLineItem(): BelongsTo
{
return $this->belongsTo(OrderLineItem::class);
}
this is where get the data relationship
public function outboundOrders(): HasMany
{
return $this->hasMany(OutboundOrders::class);
}
here's the model
protected $table = 'outbound_order_line_items';
public $timestamps = true;
protected $fillable = [
'id',
'outbound_line_item_name',
and here is the resource
Tables\Columns\TextColumn::make('outbound_order_line_item.outbound_line_item_name')->label('Line Item'),