Get if the user has selected any new option from select in laravel

662 Views Asked by At

I am using laravel backpack and in my update function there is a form which could be edited using edit button of laravel which calls the update function.

and in my form I have an expiry_date and a month field which helps my client to update the subscription of his user. All is working fine and here is the code of my update function:

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;
}

NOW THE PROBLEM IS:

  • whenever my client clicks on edit button and edits the fields other than month and clicks on update, it calculates the expiry date again but my client clicked the button to update some other details of his user.

I want to ask if there is any method or something which could help me in finding that :

if the user has clicked on select of month and has selected a option from the select then only execute the adding month to expiry_date code and if not then only update the details of the user without affecting the original expiry_date.

If this is not the way please help me in finding the correct way.

Thanks and Regards

1

There are 1 best solutions below

0
On BEST ANSWER

As no body is answering the question.

I solved it by:

1) Allowing null values in months field.

2) And Setting a default value of month field to '0'.

Like this:

   '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) and then in update function I called the last value of the month if it is '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;
}

And this solved the problem!! Thanks