使用数据库查询填充背包(Backpack)的select_from_array选项()。

huangapple go评论59阅读模式
英文:

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_storevalue_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 &lt;table&gt;CrudController.php. It's coded for a field called form_trx in &lt;table&gt;.

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 &lt;table&gt;; turn them into an associative array; and supply that array to a select_from_array field for form_trx in the Backpack controller for &lt;table&gt;.

First, at the top of &lt;table&gt;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=&gt;value pairs
        $options = DB::table(&#39;lookups&#39;)
                    -&gt;where(&#39;type_lookup&#39;, &#39;form_trx&#39;) 
                    -&gt;orderBy(&#39;sequence_lookup&#39;)
                    -&gt;pluck(&#39;value_display&#39;, &#39;value_store&#39;)
                    -&gt;toArray() 
                    ;

        // Set up the form_trx field in Backpack&#39;s CRUD controller
        CRUD::field(&#39;form_trx&#39;) 
        -&gt; label(&#39;Form&#39;)
        -&gt; type(&#39;select_from_array&#39;)  // the type of Backpack field you want
        -&gt; allows_null(false)
        -&gt; 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().

huangapple
  • 本文由 发表于 2023年2月27日 00:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定