How do i format a relation column and return html? #1919
-
this is my DemandeModel: <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Demande extends Model
{
use HasFactory;
function stage()
{
return $this->belongsTo(Stage::class);
}
} i want to get the Stage name property and return it inside badge. how do i do that? Attempt to read property "name" on null DemandeTable.php <?php
namespace App\Livewire;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use App\Models\Demande;
use App\Models\Stage;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateRangeFilter;
class DemandeTable extends DataTableComponent
{
protected $model = Demande::class;
public function configure(): void
{
$this->setPrimaryKey('id');
}
public function filters(): array
{
$stages = Stage::all()->toArray();
$stageFilter = [];
foreach ($stages as $stage) {
$stageFilter[$stage['id']] = $stage['name'];
}
return [
SelectFilter::make('Status')
->options($stageFilter)
->filter(function (Builder $builder, string $value) {
$builder->whereRelation('stage', 'id', $value);
}),
];
}
public function builder(): Builder
{
return Demande::query()->with("stage"); // tried here to get demande with stage
}
public function columns(): array
{
return [
Column::make("Id", "id")
Column::make("Status")
->sortable()
->searchable()
->label(
fn($row, Column $column) => '<span class="bg-blue-100 text-blue-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded dark:bg-blue-900 dark:text-blue-300">' . $row->stage->name . '</span>
'
)
->html(),
];
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
HashashinMaster
Sep 3, 2024
Replies: 1 comment
-
found it, i just used format method instead of label : Column::make("Status", 'stage.name')
->sortable()
->searchable()
->format(
function ($value, $row, Column $column) {
return Helpers::generateStageBadge($value); // value here is the stage.name
}
)
->html(), |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
HashashinMaster
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
found it, i just used format method instead of label :