Home > OS >  Even after updating the request parameters it's not taking
Even after updating the request parameters it's not taking

Time:11-17

I am taking the request from the user which contains limit,pagination,search and I am storing the request details in one variable called user_data.

Now I am updating one of the request value to some other value, it's updating the user_data variable also but actual query is running before-update value, why it's not taking update value , please help me to fix the issue..

$user_data=$request->all();
   //before update $user_data=array("limit"=>25,"search"="99.9");
$request->merge(["search"=> ($request->search *100)]);
   //When I debug the updated value is showing array("limit"=>25,"search"="9990");

$books=Apiato::call('Book@GetAllBooks',[$user_data,$request->from_date,$request->to_date]);

// When I dd($books) its taking the query value as `99.9` only

GetAllBooks.php

 public function run(Array $data, $from_date=null, $to_date=null)
    {
        if(!is_null($from_date) && !is_null($to_date))
        {
            $this->repository->pushCriteria(new WhereDateBetweenCriteria('created_at', $from_date, $to_date));
            return $this->repository->all();
        }
        else {
            return $this->repository->paginate();
        }
    }

My requirement is it should takes the updated value not the user entered value

CodePudding user response:

You're taking $data before updating the $request. Try this,

$request->merge(["search"=> ($request->search *100)]);
$data = $request->all();

CodePudding user response:

Why bother with $user_data at all? If you're only defining a variable for a single use, consider not defining it at all. Also, in your code, you have [$user_data], which has an issue. $user_data is already an array; if you wrap it in [], you get a nested array, which is likely incompatible with your Book@getAllBooks call.

This code should work:

$request->merge(['search' => $request->input('search') * 100]);
$books = Apiato::call('Book@GetAllBooks', $request->input());
  • Related