7

How can i create a Nova filter that will allow me to filter my Question resource by another resource called Module?

The question belongsTo to the module (module_id is FK on Questions).

So for the apply method i have:

public function apply(Request $request, $query, $value)
{
    return $query->where('module_id', $value);
}

I'm struggling with the options method. I would like to have the module->name as the key and the module->id as the value but would like to display all modules.

Adnan
  • 3,129
  • 6
  • 31
  • 36

1 Answers1

9

Use Module model to retrieve all & use collection method pluck to get name => id as key value pair.

public function options(Request $request)
{
    $models = \App\Module::all();
    return $models->pluck('id', 'name')->all();
}
Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70