laravel 更改LeadController方法以读取user_id标签而不是Value

x33g5p2x  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(97)

我有我的CRM这是工作正常的代码,但下拉菜单显示的是用户ID,而不是用户名.
现在,我希望它显示用户名:

// LeadController code:

public function massUpdateSalesperson()
{
    $data = request()->all();

    foreach ($data['rows'] as $leadId) {
        $lead = $this->leadRepository->find($leadId);
        $lead->update(['user_id' => Arr::get($data, 'value.value')]);

        Event::dispatch('lead.update.before', $leadId);
    }

    return response()->json([
        'message' => trans('admin::app.response.update-success', ['name' => trans('admin::app.leads.title')])
    ]);
}

这个DataGrid代码:

$this->addMassAction([
    'type'    => 'update',
    'label'   => trans('admin::app.datagrid.update_salesperson'),
    'action'  => route('admin.leads.mass_update_salesperson'),
    'method'  => 'PUT',
    'options' =>$this->getUserDropdownOptions(),
]);

当我进行以下更改时,它会将leadby值更新为“Null”,而不是使用新的Sales Person进行更新。
LeadDataGrid代码:

public function prepareMassActions()
{

    $users = User::all();
    $UserName = [];
    
    foreach ($users as $user) {
        $UserName[$user['name']] = $user['id'];
    }

     $this->addMassAction([
        'type'    => 'update',
        'label'   => trans('admin::app.datagrid.update_salesperson'),
        'action'  => route('admin.leads.mass_update_salesperson'),
        'method'  => 'PUT',
        'options' => $UserName,
    ]);
}

电极导线控制器代码:

public function massUpdateSalesperson()
{
    $data = request()->all();

    foreach ($data['rows'] as $leadId) {
        $lead = $this->leadRepository->find($leadId);
        $lead->update(['user_id' => Arr::get($data, 'value.value')]);
        Event::dispatch('lead.update.before', $leadId);
    }

    return response()->json([
        'message' => trans('admin::app.response.update-success', ['name' => trans('admin::app.leads.title')])
    ]);
}

注意,当我将value.value更改为value.label时,我仍然得到相同的结果。
下面是getUserDropdownOptions()代码

public function getUserDropdownOptions(): array
{
    $options = app(\Webkul\User\Repositories\UserRepository::class)
        ->get(['id as value', 'name as label'])
        ->map(function ($item, $key) {
            $item->disabled = false;
            $item->selected = false;
            return $item;
        })
        ->pluck('label', 'value') // Modify this line to pluck the name as 'label' and the ID as 'value'
        ->toArray();

    return [
        [
            'label'    => __('admin::app.common.select-users'),
            'value'    => '',
            'disabled' => true,
            'selected' => true,
        ],
        ...$options
    ];
}
50pmv0ei

50pmv0ei1#

您似乎正在使用bagisto/bagisto包。你的pluck()参数需要是反的。应该是这样的:

public function getUserDropdownOptions(): array
{
    $options = app(\Webkul\User\Repositories\UserRepository::class)
        ->get(['id as value', 'name as label'])
        ->map(function ($item, $key) {
            $item->disabled = false;
            $item->selected = false;
            return $item;
        })
        ->pluck('value', 'label') // Modify this line to pluck the name as 'label' and the ID as 'value'
        ->toArray();

    return [
        'label'    => __('admin::app.common.select-users'),
        'value'    => '',
        'disabled' => true,
        'selected' => true,
        'options' => $options
    ];
}

供参考:
拔毛
How to create Datagrid in Bagisto

相关问题