Skip to main content

Field Callbacks

There are multiple callbacks you can define on your fields. Here is a list with the arguments they receive:

  • filterUsing - filterUsing (Builder $query, $value, QoreResource $resource)
  • sortUsing - sortUsing (Builder $query, string $direction, QoreResource $resource)
  • exportUsing - exportUsing ($value, $model, string $type)
  • displayUsing - displayUsing ($value, Model $model, $type)
  • summarizeUsing - summarizeUsing (Builder $query)
  • fillUsing - fillUsing (Model $model, array $data)
  • fillLazyUsing - fillLazyUsing (Model $model, array $data)
  • fillMissing - fillMissing (Model $model, array $data)
  • fillLazyMissing - fillLazyMissing (Model $model, array $data)

Fill & Fill lazy

When a resource gets created or updated, the fillUsing will be called on each field.

However, sometimes (for example when creating a resource), you will not have a id available yet for your newly created model. When attaching relationships to a model you should use fillLazyUsing. This will be called after save has been called on the model.

class Example extends Field
{
public function __construct(string $label, string $name, string $otherResource = null)
{
parent::__construct($label, $name, $otherResource);

$this->fillUsing(function (Model $model, array $data) {
$model->{$this->column} = $data[$this->name];
});

// Or lazy:

$this->fillLazyUsing(function (Model $model, array $data) {
$sync = $model->{$this->relationName}()->sync(
$this->otherResource->indexQuery()->whereIn('id', $data[$this->column])->get()->pluck('id')
);

$this->logSync($model, $sync);
})
}
}