Refine will be launching soon 🎉 Sign up for the early access list to stay up to date.
Refine for Laravel
A package by Hammerstone

Meta

Hammerstone Refine has not launched yet. Sign up for the early access list to stay up to date!

Every application is going to have different needs regarding what data needs to be sent to the frontend. To accommodate for that, we've provided a withMeta function that allows you to attach any arbitrary information to a condition.

Attaching Meta

Consider the scenario where you want to send hint text to your frontend to display in the UI. There is no out of the box feature for that, but you could accomplish that by putting the information into the meta array, like so:

1TextCondition::make('name')
2 ->withMeta([
3 'hint' => "The employee's first and last name."
4 ]);

Now when the condition is sent to the frontend, it will include the hint in the meta key:

1{
2 "id": "name",
3 "display": "Name",
4 "type": "text",
5 "meta": {
6 "hint": "The employee's first and last name."
7 }
8}
Code highlighting powered by torchlight.dev, a Hammerstone product.

Now you can use this hint value anywhere you want on the frontend.

If you're only adding a single piece of meta, you can pass the key as the first parameter and the value as the second:

1TextCondition::make('name')
2 ->withMeta('hint', "The employee's first and last name.");

Calculated Meta

If you ever need to, you can pass a closure for the value using either style:

1TextCondition::make('name')
2 ->withMeta('something_complex', function() {
3 // Some calculated value
4 });
5 
6// Or...
7 
8TextCondition::make('name')
9 ->withMeta([
10 'something_complex' => function() {
11 // Some calculated value
12 }
13 ]);

Hiding Data From the Frontend

There may be times when you need to add something to a condition's meta but you don't want that information being sent out to the frontend.

Any meta key you prefix with an underscore gets removed before being sent to the frontend.

1TextCondition::make('name')
2 ->withMeta([
3 '_secret' => 'Never makes it to the frontend'
4 ]);

We take advantage of this underscore convention in the OptionCondition to separate the option's id and value. Go here to read more.

Adding Macros

Following the example of "hint text" from above, you may find yourself creating conventions that utilize the storing of information in the meta key and decide that you need to formalize the convention.

Because the base Condition class uses Laravel's Macroable trait, you can extend the class's functionality without having to crack it open.

In our hint text scenario, you could use a macro to create a proper hint method.

1Condition::macro('hint', function ($text) {
2 return $this->withMeta('hint', $text);
3});

Now you can just call hint and you don't have to futz about with adding it to meta every time, or potentially adding the meta under the wrong key.

1TextCondition::make('name')
2 ->hint("The employee's first and last name.");