How do I show the data from the description of another table and not a number?

how to work relationships
No description
No description
Solution:
thanks
Jump to solution
24 Replies
Brian Kidd
Brian Kidd10mo ago
Does your model have a relationship for this?
Yurikaso
YurikasoOP10mo ago
if ($isMultiline) {
<?php

namespace App\Models;

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

class Asignacion extends Model
{
use HasFactory;

protected $table = 'asignaciones';

protected $fillable = [
'stock_id',
'nombre',
'descripcion' => 'string',
'serial',
'cantidad'
];

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

}
if ($isMultiline) {
<?php

namespace App\Models;

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

class Asignacion extends Model
{
use HasFactory;

protected $table = 'asignaciones';

protected $fillable = [
'stock_id',
'nombre',
'descripcion' => 'string',
'serial',
'cantidad'
];

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

}
trymedo
trymedo10mo ago
Looks like your making a Select form item for a string field. Try TextInput instead 👍
Yurikaso
YurikasoOP10mo ago
if ($isMultiline) {
<?php

use App\Models\Stock;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('asignaciones', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Stock::class)->constrained()->nullable(); // Clave foránea hacia la tabla 'Stock'
$table->string('nombre')->nullable();
$table->string('descripcion')->nullable();
$table->string('serial')->nullable();
$table->string('cantidad')->nullable();
$table->timestamps();
});
}

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

}
if ($isMultiline) {
<?php

use App\Models\Stock;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('asignaciones', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Stock::class)->constrained()->nullable(); // Clave foránea hacia la tabla 'Stock'
$table->string('nombre')->nullable();
$table->string('descripcion')->nullable();
$table->string('serial')->nullable();
$table->string('cantidad')->nullable();
$table->timestamps();
});
}

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

}
my table
trymedo
trymedo10mo ago
Ah I just re-read the title of the post... if you have a Descripcion model, then your table will need a descripcion_id. Then your Asignacion model will need to have the relationship setup correctly like so
public function descripcion(): HasOne
{
return $this->hasOne(Descripcion::class);
}
public function descripcion(): HasOne
{
return $this->hasOne(Descripcion::class);
}
Yurikaso
YurikasoOP10mo ago
if ($isMultiline) {
<?php

class AsignacionesResource extends Resource
{
protected static ?string $model = Asignacion::class;

protected static ?string $pluralModelLabel = 'Asignaciones';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

protected static ?string $navigationLabel = 'Asignaciones';

public static function form(Form $form): Form
{
return $form
->schema([
Section::make()
->schema([
TextInput::make('nombre')
->placeholder('Escribe un nombre')
->maxLength(255)
->required(),
TextInput::make('serial')
->placeholder('Escribe un número de serial')
->maxLength(255)
->required(),
TextInput::make('cantidad')
->placeholder('Escribe una cantidad')
->minValue(1)
->maxLength(255)
->numeric()
->required(),
Select::make('stock_id')
->label('Descripción stock')
->relationship('stock', 'descripcion')
->required()
])
]);
}

if ($isMultiline) {
<?php

class AsignacionesResource extends Resource
{
protected static ?string $model = Asignacion::class;

protected static ?string $pluralModelLabel = 'Asignaciones';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

protected static ?string $navigationLabel = 'Asignaciones';

public static function form(Form $form): Form
{
return $form
->schema([
Section::make()
->schema([
TextInput::make('nombre')
->placeholder('Escribe un nombre')
->maxLength(255)
->required(),
TextInput::make('serial')
->placeholder('Escribe un número de serial')
->maxLength(255)
->required(),
TextInput::make('cantidad')
->placeholder('Escribe una cantidad')
->minValue(1)
->maxLength(255)
->numeric()
->required(),
Select::make('stock_id')
->label('Descripción stock')
->relationship('stock', 'descripcion')
->required()
])
]);
}

my form I understand but what I want is to have the data from the description field of my stock table, that are equal to my description field of my asignaciones table. but it converts them into a number in the database how much do I put the select button
trymedo
trymedo10mo ago
Is it adding the id of the descripcion in that column, but you want to add the descripcion value or something? I don't know what the Descripcion model looks like, but you can use dot syntax too so maybe something like descripcion.name instead? Sorry, I'm not sure I fully understand what you're trying to achieve.
Yurikaso
YurikasoOP10mo ago
What I'm trying to do is simply have the description field of my stock table = the description field of my assignments table, but when I create a select in my resource assignments, it sends it to the database as a number and I want the text
No description
trymedo
trymedo10mo ago
So what does your descripciones (or whatever it's called) table look like?
Yurikaso
YurikasoOP10mo ago
like this
if ($isMultiline) {
Select::make('descripcion')
->label('Descripción stock')
->relationship('stock', 'descripcion')
->required()
}
if ($isMultiline) {
Select::make('descripcion')
->label('Descripción stock')
->relationship('stock', 'descripcion')
->required()
}
Yurikaso
YurikasoOP10mo ago
this is what I want T_T
No description
Yurikaso
YurikasoOP10mo ago
That the same values of the field be sent in the database, because there are no problems in the panel
Yurikaso
YurikasoOP10mo ago
because it is displayed correctly and searched correctly in the form
No description
trymedo
trymedo10mo ago
In that table, are you linking it to stock.descripcion?
Yurikaso
YurikasoOP10mo ago
yeah in the column
trymedo
trymedo10mo ago
So can you do the same in your form? 🤔
Yurikaso
YurikasoOP10mo ago
Is this possible? (Stock) Field (Descripcion) = (Asignaciones) Field (Descripcion) and not a number T_T only real record T_T TEXT!
trymedo
trymedo10mo ago
I'm fairly new to Filament but maybe try something like this in your EditAssignacion.php file:
protected function mutateFormDataBeforeSave(array $data): array
{
$data['descripcion'] = \App\Models\Stock::findOrFail($data['stock_id'])->descripcion;

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['descripcion'] = \App\Models\Stock::findOrFail($data['stock_id'])->descripcion;

return $data;
}
Yurikaso
YurikasoOP10mo ago
No description
Yurikaso
YurikasoOP10mo ago
ITS WORK! :DDDDD
No description
Yurikaso
YurikasoOP10mo ago
How do you know where you get those types of solutions? I'm new too
trymedo
trymedo10mo ago
Well Filament is very well documented but sometimes you need to have some kind of clue as to what you're looking for. I had recently used the afterSave hook, so I just kind of guessed that there would also be a beforeSave or something and eventually found this: https://filamentphp.com/docs/3.x/panels/resources/editing-records#customizing-data-before-saving
trymedo
trymedo10mo ago
On a side note; Doing it this way means that if the stock descripcion gets updated, the assignacion descipcion won't match it anymore and they're going to be out of sync. If you want them to always match then you're better off not storing the descripcion in that table at all and instead always fetching the $assignacion->stock->descripcion instead. Of course, I don't know what your specific use-case is, so there's probably a reason you're copying the data over in this way. Either way, I'm happy you've got it working 😀
Solution
Yurikaso
Yurikaso10mo ago
thanks
Want results from more Discord servers?
Add your server