F
Filament16mo ago
jay02k

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
Kiran Timsina
Kiran Timsina16mo ago
TextColumn::make('user.name') assuming that you have a relation user() on Order model.
jay02k
jay02kOP16mo ago
i see i think i am missing that part to add relation on order model may i know how i will do that
kodekodimas
kodekodimas16mo ago
to handle you can override function handleRecordCreation(array $data) on create pages, and write your custom logic
jay02k
jay02kOP16mo ago
do we have any sample on filament docs? i have order resource and order model order resource were i display data from database
Vp
Vp16mo ago
I am not sure what you want exactly.. so here is an example (not tested)
//orders table
$table->foreignId('user_id');

// order model
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

// table
TextColumn::make('user.name')

// form
Forms\Components\Select::make('user_id')
->label('User')
->options(User::all()->pluck('name', 'id'))
->searchable()
//orders table
$table->foreignId('user_id');

// order model
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

// table
TextColumn::make('user.name')

// form
Forms\Components\Select::make('user_id')
->label('User')
->options(User::all()->pluck('name', 'id'))
->searchable()
jay02k
jay02kOP16mo ago
i see will this need to run migratio or no need? becasue i don't want to modify the database table
kodekodimas
kodekodimas16mo ago
do you want to insert data to user table and order table from order resource?
jay02k
jay02kOP16mo ago
i just want to pull/fetch data from users table and display on order resource just want to populate the table from existing data
kodekodimas
kodekodimas16mo ago
on order model add relation to user then on your OrderResource > table function add TextColumn::make('user.column')
jay02k
jay02kOP16mo ago
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
petrisorcraciun
petrisorcraciun16mo ago
yes
jay02k
jay02kOP16mo ago
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); }
kodekodimas
kodekodimas16mo ago
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); }
jay02k
jay02kOP16mo ago
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'),
Want results from more Discord servers?
Add your server