skip to Main Content

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


  1. Chosen as BEST ANSWER

    Thank you, @PedroX. You got me onto the right track.

    I now have a working pattern for populating a Backpack select_from_array field's options() method with key=>value pairs plucked from a different table (called lookups 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 called form_trx in <table>.

    It also relies on an additional table called lookups that contains four columns: value_store and value_display are a key and a value, respectively, for the field named in type_lookup. And sequence_lookup controls the order in which the values will appear in the select_from_array field for the field named in type_lookup.

    So here's how to collect all the keys and values stored in lookups that are relevant to the form_trx field in <table>; turn them into an associative array; and supply that array to a select_from_array field for form_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 the lookups table:

    use IlluminateSupportFacadesDB ;
    

    Then, in the setupCreateOperation() function, add the following:

            // Fetch a collection of key=>value pairs
            $options = DB::table('lookups')
                        ->where('type_lookup', 'form_trx') 
                        ->orderBy('sequence_lookup')
                        ->pluck('value_display', 'value_store')
                        ->toArray() 
                        ;
    
            // Set up the form_trx field in Backpack's CRUD controller
            CRUD::field('form_trx') 
            -> label('Form')
            -> type('select_from_array')  // the type of Backpack field you want
            -> allows_null(false)
            -> options($options)  // Backpack will display the value, store the key
            ; 
    
    

    You can use the same code in the setupListOperation() function, too. But you'll have to change CRUD::field() to CRUD::column().


  2. you should pass an array not a closure to the options:

    
    $options = ['array', 'of', 'options'];
    
    CRUD::field('my_field')->options($options);
    
    

    Cheers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search