I have an input like below.
array:4 [
"_token" => "evktHCfCNZVQMNYXzntfHZkdNLZFqvOoYgU3yPKy"
"name" => "Name"
"orderId" => "5cb5943a6733a1555403834"
"amount" => null
]
which I stored to a session like this:
public function store(Request $request)
{
request()->session()->put('bookingInfo', $request->input());
return redirect()->route('book.checkout');
}
Now I want to change the amount value in the session. How I am going to achieve that?
To update the value, you need to retrieve the values from session and update it again.
To do that, you need to do
$booking_info = $request->session()->get('bookingInfo');
Then you will got an array back. Update it like a normal array
$booking_info["amount"] = 10; // anything you wanted
If you need to put it back into session again you can do this
$request->session()->put('bookingInfo', $booking_info);
EDIT
If you want the ability to update partially, you would need to store it separately like this.
$input = $request->all();
$request->session()->put('bookingInfo.name', $input['name']);
$request->session()->put('bookingInfo.orderId', $input['orderId']);
$request->session()->put('bookingInfo.amount', $input['amount']);
Laravel 5+
// Via a request instance...
$request->session()->put('amount', 'value');
// Via the global helper...
session(['amount' => 'value']);
// For retrieve
session('amount');
Related
I want to use laravels FormRequest to validate before updating some fields. This works fine if i just use:
User::find($application->userid)->fill($request->only('first_name'...
but the request also contains sub array ($request->programmeData).
array:2 [▼
"programme_id" => 5
"programme_title" => "some programme title"
]
if i try access that the same way i get 'Call to a member function only() on array':
Course::find($application->userid)->fill($request->programmeData->only('programme_id...
I've tried a handful of things, but not sure best way to go with this?
Update
I'm now using a foreach loop to save two items in the array. the example below saves the second value for both user_ids. any reason this isn't saving the first value for the first user_id?
foreach ($request->programmeData['userProgrammes'] as $key=>$userProgrammes) {
Course::where('application_id', $application->id)->get()[$key]->fill(Arr::only($request->programmeData['userProgrammes'][$key], ['programme_id']))->save();
}
but nothing updates. Any ideas on this one?
You can use Array::only() helper for this:
foreach ($request->programmeData['userProgrammes'] as $key=>$userProgrammes) {
Course::where('application_id', $application->id)->first()->fill([
$key => Arr::only($request->programmeData['userProgrammes'][$key], ['programme_id'])
])->save();
// or
$course = Course::where('application_id', $application->id)->first()
$course->$key = Arr::only($request->programmeData['userProgrammes'][$key], ['programme_id']);
$course->save();
}
//Arr::only($request->programmeData, ['programme_id', ...]);
I am working on a movie WebApp. The User selects Movies and it gets added to the database. the column movie_list is a JSON because
I want that every time a User add a movie it gets added to the Array and if its already in it I want that it doesn't get added to the array
The problem is instead of adding it to the existing array it will sometime overwrite the Array, just add a nested Array or just create a custom key (0,1,2,3).
I tried to function like
array_merge and array_add
// It does overwrite it
array['checkedMovie'] = 3
Also thought about for each, but sadly dont know how to realize it.
My Brain is squeezed up.
public function update(Request $request, TrackMovie $trackMovie)
{
$currentCheckedMoviesArray = DB::table('track_movies')->where('user_id', $trackMovie->user_id)->get('checkedMovie');
$currentCheckedMoviesArray = json_decode($currentCheckedMoviesArray, true)[0];
$newarraychecked=array_add($currentCheckedMoviesArray,$currentCheckedMoviesArray['checkedMovie'], $trackMovie->checkedMovie);
return dd($newarraychecked);
$current_length = DB::table('track_movies')->where('user_id', $trackMovie>user_id)->value('lengthOfMovie');
DB::table('track_movies')->where('user_id', $trackMovie->user_id)->update([
'lengthOfMovie' => $current_length + $trackMovie->lengthOfMovie,
'checkedMovie' => $newarraychecked;
]);
return dd(TrackMovie::all());
}
To make it a bit clearly I edited this because I think its because of my formatting.
$currentCheckedMoviesArray = json_decode($currentCheckedMoviesArray, true)[0];
// DD Result
array:1 [▼
"checkedMovie" => "["1", "2"]"
]
$trackMovie->checkedMovie
//DD Result
array:2 [▼
0 => "2"
1 => "4"
]
$newarraychecked=Arr::collapse($currentCheckedMoviesArray, $trackMovie->checkedMovie);
//DD Result
[]
Actual Result:
This is the result what I get on the above code
array:1 [▼
"checkedMovie" => "["1", "2"]"
]
There some more because I tested many things
array:1 [▼
"checkedMovie" => "["1", "2"]"
1 => "2"
2 => "4"
]
Expected Result:
The User is checking some movies.
// He already has some movies
checkedMovie = ["1","2","3"]
Now the Application Checks if it already existed the movie in the Database.
If it does not contain in the database I want to add it. User selects Movie ID (5,6)
checkedMovie = ["1","2","3","5","6"]
After that, it will overwrite the Database column value
If I have forgotten something to add up to the question, please comment it so I can edit the question!
Try with the following:
$checkedMovie=array(1,2,3);
$selectedMovie=array(3,4,5,6);
$checkedMovie=array_unique(array_merge($checkedMovie,$selectedMovie));
You can just push the new values to the array and then use array_unique to filter out the duplicates
Example:
public function update(Request $request, TrackMovie $trackMovie)
{
//Get Current Array from DB
$currentCheckedMoviesArray = DB::table('track_movies')->where('user_id', $trackMovie->user_id)->get('checkedMovie');
$currentCheckedMoviesArray = json_decode($currentCheckedMoviesArray, true)[0];
//Decode JSON array
$currentCheckedMoviesArray['checkedMovie'] = json_decode($currentCheckedMoviesArray['checkedMovie'])
//Push the new Movie to the array
$currentCheckedMoviesArray['checkedMovie'][] = $trackMovie->checkedMovie;
//Remove duplicates from the array
$newarraychecked = array_unique($currentCheckedMoviesArray['checkedMovie']);
....
}
I am attempting to modify a Laravel Request object using merge to update the key trial_end.
I am doing this using the following code...
if ($this->request->get('trial_end', '')) {
$this->request->merge(array('trial_end' => 'test'));
}
dd($this->request->all(), $this->request->get('trial_end'));
I expect $this->request->get('trial_end') to be test, but it is not. $this->request->all() returns what I expected.
Result of die dump
array:1 [
"trial_end" => "test"
]
"12/4/2018"
How come it is not returning the updated value?
Figured it out. The solution was to change
$this->request->get('trial_end');
to
$this->request->input('trial_end');
This works because input() adds the data in all() to getInputSource()->all() before doing a data_get on that, whereas get() just performs a data_get on the input parameters (pre-modifications).
New code (with a change suggested by Alex)
if ($this->request->has('trial_end')) {
$this->request->merge(['trial_end' => 'test']);
}
dd($this->request->all(), $this->request->input('trial_end'));
New results
array:1 [
"trial_end" => "test"
]
"test"
Hope this helps any others that come across this issue.
the problem it's not with the assignment, but with the comparison, here's my personally best way to check if a value in the request has been set.
public function test(Request $request){
if (!$request->has('trial_end')) { //this is what you have wrong
$request->merge(array('trial_end' => 'test'));
}
return $request->get('trial_end');
}
greetings
Here is the problem I have a session
session('products')
this is actually an array that contains id
session('products')
array:4 [▼
0 => "1"
1 => "2"
2 => "4"
3 => "1"
]
Now I want to delete lets say 4 How do I do that? I tried method
session()->pull($product, 'products');
But it didn't work!
Other solution
session()->forget('products', $product);
it also didn't work
You AFAIR have to firstly retrieve whole array, edit it and then set it again. If you want to delete by product ID, which is as I assume an array value, you can use this: PHP array delete by value (not key)
$products = session()->pull('products', []); // Second argument is a default value
if(($key = array_search($idToDelete, $products)) !== false) {
unset($products[$key]);
}
session()->put('products', $products);
Misunderstood question
Session::pull takes first parameter as the item do delete and second as the default value to return. You have mistaken the order of arguments. Try:
session()->pull('products'); // You can specify second argument if you need default value
As I can see in source, Session::forget expects string or array, so you should specify only the first parameter:
session()->forget('products');
This method isn't tested:
Session::forget('products.' . $i);
note the dot notation here, its worth the try. If that isnt working, you can always do this:
$products = Session::get('products'); // Get the array
unset($product[$index]); // Unset the index you want
Session::set('products', $products); // Set the array again
You can do it in the following way in case you have a function to delete an item from a cart
public function deleteProductoCart(Producto $producto) {
$cart = \Session::get('products');
unset($cart[$producto->id]);
\Session::put('products', $cart);
return redirect()->route('tu-ruta');
}
I am grabbing the values from the parameters in the URL domain.com?para=value in the controller using
Input:all()
Is there a way to add more values to the Input:all() in the controller?
I have tried $_POST['para'] = "value and $_GET['para'] = "value" but no luck.
I've gone through the docs but cannot find anything.
Thanks
More Info
Here is what is returned
{
"param_1" => "value",
"param_2" => "value",
"param_3" => "value",
}
I would like to add another param into the Input:all()
{
"param_1" => "value",
"param_2" => "value",
"param_3" => "value",
"NEW_PARAM" => "NEW VALUE",
}
In laravel 5, you can use
Request::merge(['New Key' => 'New Value']);
or by using request() helper
request()->merge(['New Key' => 'New Value']);
You should never need to add anything to Input. You should assign Input like so...
$arr = Input::all();
And then add to $arr like so...
$arr['whatever'] = 'whatever';
If you need to get that value in another part of the stack, try to pass it through yourself.
Cheers.
Best way to add data into the input::all() in laravel.
Solution 1
add Request package at the top of the page.
use Request;
Then add following code into your controller.
Request::merge(['new_key' => 'new_value']);
Solution 2
You can assign all the Input::all(); to a variable and then you can add new data to the variable. Like below.
$all_input = Input::all();
$all_input['new_key'] = 'new_value';
Add an input value on the fly inside a request instance
public function store(Request $request){
$request->request->add(['new_key' => 'new_value']);
}
Remove data from an input value on the fly inside a request instance
public function store(Request $request){
$request->request->remove('key');
}