I’ve defined a Backpack select_from_array
column for the field form_trx
in the table transactions
. I want to populate the options
array for that Backpack column with data that’s stored in two columns (called value_store
and value_display
) of another database table (called lookups
). I thought this would work:
from TransactionCrudController.php
CRUD::column('form_trx')
-> type('select_from_array')
-> options(
function ()
{ return
DB::table('lookups')
->where('type_lookup', 'form_trx')
->value('value_store', 'value_display')
->toArray();
});
But it fails, saying
Cannot use object of type Closure as array.
I think I’ve got some PHP details wrong in trying to supply an array to the options() method. Can you tell me the right syntax? Thanks!
2
Answers
Thank you, @PedroX. You got me onto the right track.
I now have a working pattern for populating a Backpack
select_from_array
field'soptions()
method with key=>value pairs plucked from a different table (calledlookups
below) using an Eloquent query. Since that pattern may be helpful to others, I've shared it below.This logic belongs in
<table>CrudController.php
. It's coded for a field calledform_trx
in<table>
.It also relies on an additional table called
lookups
that contains four columns:value_store
andvalue_display
are a key and a value, respectively, for the field named intype_lookup
. Andsequence_lookup
controls the order in which the values will appear in theselect_from_array
field for the field named intype_lookup
.So here's how to collect all the keys and values stored in
lookups
that are relevant to theform_trx
field in<table>
; turn them into an associative array; and supply that array to aselect_from_array
field forform_trx
in the Backpack controller for<table>
.First, at the top of
<table>CrudController.php
, add this line so we can use the DB facade to query thelookups
table:Then, in the
setupCreateOperation()
function, add the following:You can use the same code in the
setupListOperation()
function, too. But you'll have to changeCRUD::field()
toCRUD::column()
.you should pass an array not a closure to the options:
Cheers