【问题标题】:Get if the user has selected any new option from select in laravel获取用户是否从 laravel 中的 select 中选择了任何新选项
【发布时间】:2017-09-17 13:02:55
【问题描述】:

我正在使用 laravel 背包,在我的更新功能中有一个可以使用 laravel 的 edit 按钮编辑的表单,它调用了更新函数。

在我的表单中,我有一个 expiry_date 和一个 month 字段,可帮助我的客户更新其用户的订阅。一切正常,这是我的更新功能的代码:

public function update(UpdateRequest $request)
{

    date_default_timezone_set('asia/calcutta');
    $month = $request->month;
    $expiry = new Carbon($request->expiry_date);
    $expiry_date = $expiry->addMonths($month);            
    $request['expiry_date'] = $expiry_date;

    // your additional operations before save here
    $redirect_location = parent::updateCrud($request);
    // your additional operations after save here
    // use $this->data['entry'] or $this->crud->entry
    return $redirect_location;
}

现在的问题是:

  • 每当我的客户单击编辑按钮并编辑除month 以外的字段并单击更新时,它会再次计算到期日期,但我的客户单击该按钮以更新其用户的其他一些详细信息。

我想问是否有任何方法或东西可以帮助我找到:

如果用户点击了select of month并从select中选择了一个选项然后只执行adding month to expiry_date code,如果不是那么只更新用户的详细信息而不影响原始的expiry_date .

如果这不是方法,请帮助我找到正确的方法。

感谢和问候

【问题讨论】:

    标签: php laravel laravel-backpack


    【解决方案1】:

    因为没有人回答这个问题。

    我解决了这个问题:

    1) 月份字段中允许空值。

    2) 并将月份字段的默认值设置为“0”。

    像这样:

       'name' => 'month',
            'label' => "Months",
            'type' => 'select2_from_array',
            'options' => ['0' => '0','1' => '1', '3' => '3', '6' => '6', '12' => '12'],
            'allows_null' => true,
            'value' => '0'
            // 'allows_multiple' => true, // OPTIONAL; needs you to cast this to array in your model;
        ], 'update/create/both'); 
    

    3) 然后在更新函数中我调用了月份的最后一个值,如果它是'0':

    public function update(UpdateRequest $request) {
        date_default_timezone_set('asia/calcutta');
        $month = $request->month;
        if($month != 0)
        {
         $expiry = new Carbon($request->expiry_date);
        $expiry_date = $expiry->addMonths($month);            
        $request['expiry_date'] = $expiry_date;   
        }
        else
        {
            $retained_month  = Tag::find($request->id);
            $month = $retained_month->month;
            $request['month'] = $month;
    
        }
    
    
        // your additional operations before save here
        $redirect_location = parent::updateCrud($request);
        // your additional operations after save here
        // use $this->data['entry'] or $this->crud->entry
        return $redirect_location;
    }
    

    这解决了问题! 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      相关资源
      最近更新 更多