英文:
Populate Backpack select_from_array options() with DB query
问题
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!
英文:
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!
答案1
得分: 0
你应该传递一个数组而不是一个闭包给选项:
$options = ['选项', '的', '数组'];
CRUD::field('my_field')->options($options);
祝好运!
英文:
you should pass an array not a closure to the options:
$options = ['array', 'of', 'options'];
CRUD::field('my_field')->options($options);
Cheers
答案2
得分: 0
感谢你,@PedroX。你帮助我找到了正确的方法。
我现在有一个用于填充背包(Backpack)select_from_array
字段的options()
方法的工作模式,该模式从不同的表中提取键值对(下面称为lookups
)使用 Eloquent 查询。由于这个模式可能对其他人有帮助,我在下面分享了它。
这个逻辑代码应该放在<table>CrudController.php
中。它是为<table>
中名为form_trx
的字段编写的。
它还依赖于一个名为lookups
的附加表,该表包含四列:value_store
和value_display
分别是type_lookup
字段中命名的字段的键和值。sequence_lookup
控制这些值在select_from_array
字段中出现的顺序,该字段也是type_lookup
字段中命名的。
下面是如何收集与<table>
中的form_trx
字段相关的存储在lookups
中的所有键和值,将它们转化为关联数组,然后将该数组提供给<table>
的Backpack控制器中form_trx
字段的select_from_array
字段。
首先,在<table>CrudController.php
的顶部,添加以下行,以便我们可以使用DB外观来查询lookups
表:
use Illuminate\Support\Facades\DB;
然后,在setupCreateOperation()
函数中,添加以下内容:
// 获取一个键=>值对的集合
$options = DB::table('lookups')
->where('type_lookup', 'form_trx')
->orderBy('sequence_lookup')
->pluck('value_display', 'value_store')
->toArray();
// 在Backpack的CRUD控制器中设置form_trx字段
CRUD::field('form_trx')
->label('表单')
->type('select_from_array') // 你想要的Backpack字段类型
->allows_null(false)
->options($options) // Backpack会显示值,存储键
;
你也可以在setupListOperation()
函数中使用相同的代码。但是你需要将CRUD::field()
更改为CRUD::column()
。
英文:
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 Illuminate\Support\Facades\DB ;
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()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论