Add current form data as second argument to RecordFinder scope method - #1441
Add current form data as second argument to RecordFinder scope method#1441mjauvin wants to merge 1 commit into
Conversation
|
Works perfectly! Thanks!!! public function scopeWhereProduct($query, $model, $formData)
{
if ($productId = array_get($formData, 'product')) {
return $query->where('product_id', $productId);
}
return $query->where('product_id', null); // returns an empty list in my case, so they will be forced to select a product first ;)
} |
|
@mjauvin can this be merged? |
|
@AIC-BV did you test this PR as well? |
I copied the change to my local file and deployed it in production since Jan 18 |
|
@LukeTowers any objection in merging this? There's a PR to the docs also in the description. |
|
@LukeTowers Also running into this, could this be merged please? 🙏🏻 |
|
@mjauvin does this match any other scope method calls in the core? Seems somewhat arbitrary for your specific use case. @JonasPardon @AIC-BV can you provide examples of how you're using it? |
|
@LukeTowers concrete case from our webshop plugin (running patched in production since January): An product:
type: recordfinder
list: ~/plugins/aic/webshop/models/product/columns.yaml
variant:
type: recordfinder
list: ~/plugins/aic/webshop/models/variant/columns.yaml
scope: whereProductpublic function scopeWhereProduct($query, $model, $formData = [])
{
$productId = array_get($formData, 'product') ?: $model->product_id;
return $query->where('product_id', $productId); // null => empty list, forces picking a product first
}Same shape as Marc's country → state: any parent → child recordfinder pair hits this. And since PHP ignores extra arguments on userland methods, existing single-argument scopes keep working unchanged. |

It can be useful if the record finder needs some other fields in the form to filter its values.
Related: wintercms/docs#256
E.g. fields definition: