Google Sheets PHP Client API is returning empty cells if the value is formed by arrayformula in header row - google-api

Header formula of column B is
={"Name";ArrayFormula(VLOOKUP($A$3:$A; IMPORTRANGE("0000000000";"Sheet!A1:B"); 2; FALSE))}
PHP part is
$spreadsheetId = '1111111111';
$range = 'Sheet!A1:AB';
$options = ["valueRenderOption"=>"FORMATTED_VALUE"];
$response = $service->spreadsheets_values->get($spreadsheetId, $range, $options);
In the response all cells from column B is empty. How to fix this issue?
I've tried to fix this with "valueRenderOption" => "FORMATTED_VALUE" option. Does not works.

Related

Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in

I'm working on a Laravel blog and and the moment, I want to change the status of the Post status based on a certain condition using an event. In my migration, I have a post_status enum table with the statuses I want to use and I have wired up the event and the listener, also have set up the dispatchesEvents using retrieved, so I'm not really posting anything.
I want to change posts that currently have a status of Complete AND are older than 90 days to the Archived status. For this, I am using the code below. The status actually updates based on the first condition but I'm not sure the second condition is being taken into account (or is it properly defined), my hunch is that the issue is with the last line. This is the code in the listener:
public function handle(PostStatusUpdaterEvent $event)
{
$old_date = Carbon::now()->subDays(90);
Post::where(['post_status' => 3]) // 3 = Complete status
->where(['end_date', '<', $old_date])
->update(['post_status' => 4]); // 4 = Archived status
}
And the error:
lluminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: update
`posts` set `post_status` = 4, `posts`.`updated_at` = 2020-02-24 13:13:15 where
(`post_status` = 3) and (`0` = end_date and `1` = < and `2` = 2019-11-26 13:13:15))
I have the post_status field in my protected fillable array, also have the table itself protected. Also tried setting the post_status field in the protected appends array, also tried setting the table id as hidden, none of which worked. Finally, I have model and Carbon namespace imported in the listener (as in use...)
You would use array in where only if you want to run multiple checks within the same where
WHERE (`post_status` = 3 AND `end_date` = '2019-11-26 13:13:15')
Proper code for that would be
Post::where([
['post_status', 3], // 3 = Complete status
['end_date', '<', $old_date],
])
->update(['post_status' => 4]); // 4 = Archived status
To produce WHERE `post_status` = 3 AND `end_date` < '2019-11-26 13:13:15'
You would go with (without array notion)
Post::where('post_status', 3) // 3 = Complete status
->where('end_date', '<', $old_date)
->update(['post_status' => 4]); // 4 = Archived status
See Where Clauses for more info.
Change the second where to this:
where('end_date', '<', $old_date)

Update multiple rows at once Laravel Eloquent

I have the table products with the following structure.
id | name | promote
Where the column promote is of boolean type.
I want to set the value of the boolean column to 1 with the selected rows and set 0 to non-selected rows. I have the following code in the controller to handle this query.
$yes = Tour::whereIn('id', $request->promote)->get();
$no = Tour::whereNotIn('id', $request->promote)->get();
foreach ($yes as $item) {
$item->promote = 1;
$item->save();
}
foreach ($no as $item) {
$item->promote = 0;
$item->save();
}
I get following from the form request.
The above code does work but it isn't very efficient I assume. I'm looking for optional ways to achieve the result in a more efficient way.
Instead retrieving result, looping through, you can update directly,
$yes = Tour::whereIn('id', $request->promote)->update(['promote' => 1]);
$no = Tour::whereNotIn('id', $request->promote)->update(['promote' => 0]);
If you don't care about going through the Model to do the updating you can call update on the builder to update all the matched records. As this will use the builder and not the Model there will not be any model events fired:
// set them all to promote = 0
Tour::update(['promote' => 0]);
// or just set the ones that need to be 0
Tour::whereNotIn('id', $request->promote)->update(['promote' => 0]);
// set the ones you want to promote = 1
Tour::whereIn('id', $request->promote)->update(['promote' => 1]);
Just one way to give it a go.

get all rows in single query with multiple id

I have a asset_request table with fields id and request_id.
I want to select multiple rows with specific ids.
$ids = $request->ids // 5,6
I want to select only rows with ids of 5 and 6 in request table
$ids = $request->ids;
$asset_request = asset_request::whereIn('id',array($ids))->first(); //gets only 6th row.
I need to get all rows matching the given ids.
To clarify after a chat discussion with the Op:
The Op was passing back a string request, therefore, the Op needed to change the following:
$id = $request->id;
$ids = str_split(str_replace(',', '', $id));
$asset_request = asset_request::whereIn('id', $ids)->get();
First you are calling the first method which will return only the first row matched.
You need to call get method to get all rows matched.
Secondly if you are sending ids as a comma separated string you need to convert it to array using explode.
$ids = $request->ids;
$asset_requst = asset_request::whereIn('id', explode(",", $ids))->get();
DB::table('asset_request')
->whereIn('id', (array) $request->ids)
->get();
or
TableModel::whereIn('id', (array) $request->ids)->get();

code igniter updating a row with multiple values to a column

I am getting a array to my model. Say array has an value 1 and 2.
Now I need to update this to a single column 1,2 so that column will look like this
Column
1,2
foreach($x as $y){
$this->db->where('column',4);
$this->db->set('id',$data);
$this->db->update('table');
}
But this is only update 2, it is omitting 1. Where am i going wrong ?
If you want to save multiple values in a column put them in
an array and serialize them. Than save it in column.
Make the column type text.
$array['name'] = 'test';
$array['otherinfo'] = 'otherinfo';
$data = serialize($array);
foreach($x as $y)
{
$this->db->where('column',4);
$this->db->set('id',$data);
$this->db->update('table');
}
When you select you can unserialize the array using unserialize();
Use implode
For example:
$data = [1,2]
$comma_data = implode(",", $data)
Then update column with $comma_data
I was having wrong data type, I figured it out.

CodeIgniter session shows different output in different functions

In first function of my controller, I am fetching random records from mysql table using CI active record
$query = $this->db->query("SELECT DISTINCT * FROM questions WHERE `level` = '1' ORDER BY RAND() limit 0,5");
$result = $query->result_array();
and saving result in session as
// saving questions id in session
for($i = 0; $i < count($result); $i++)
{
$session['questionsId'][] = $result[$i]['qId'];
}
$this->session->set_userdata($session);
and if print session variable it shows output like:
$qIds_o = $this->session->userdata('questionsId');
var_debug($qIds_o);
Array
(
[0] => 5
[1] => 9
[2] => 3
[3] => 6
[4] => 11
)
but if I retrieve same session in another function of same controller it shows different result
$qIds = $this->session->userdata('questionsId');
var_debug($qIds);
Array
(
[0] => 2
[1] => 8
[2] => 6
[3] => 3
[4] => 5
)
and if I remove ORDER BY RAND() from mysql query like:
$this->db->query("SELECT DISTINCT * FROM questions WHERE `level` = '1' limit 0,5");
it shows same session array in both functions. Very strange.
Please guide what is going wrong....
Here is my controller script:
public function set_value(){
$query = $this->db->query("SELECT DISTINCT * FROM questions WHERE `level` = '1' ORDER BY RAND() limit 0,5");
$result = $query->result_array();
// saving questions id in session
for($i = 0; $i < count($result); $i++)
{
$session['questionsId'][] = $result[$i]['qId'];
}
$this->session->set_userdata($session);
$qIds_o = $this->session->userdata('questionsId');
var_debug($qIds_o);
}
public function get_value(){
$qIds = $this->session->userdata('questionsId');
var_debug($qIds);
}
I called set_value() on page load while once the page loaded I call get_value() using AJAX post which simply hits my_controller/get_value/ and response back to browser.
Without looking at your controller (let's call it my_controller in detail, I think what might be happening is:
(1) You call the first function, my_controller/set_value where set_value sets the session variable and you echo the result.
(2) You then call the second function, show_value that simply echos out the session variable.
What you might be doing in set_value is:
1) echo out the current session variable
2) call the query and re-set the session variable
If this is the case, then when you go to show_value (2nd function) you are looking at the recently re-set value instead of the prior value that you echoed out in the first function.
I have two questions about this sentence:
but if I retrieve same session in another function of same controller it shows different result
Is this on a new page load?
If so, is the query run again?
I'm going to assume the answer to both of these questions is yes, since you said removing RAND() gives you the same results.
You are using a combination of RAND() and LIMIT in your query, meaning you want only five rows in a random order. That means that each time the query is run (and your session data is set), it is very likely that the results will be different.
I don't know exactly what you're doing with these IDs and what sort of data set you need, so this may not be 100% perfect for your solution, but if you only need to set this session data once, you should check if it exists before running the query.
if ($this->session->userdata('questionsId') === FALSE)
{
// Run your query and set your session data here.
// Note that in CI 3.0, Session::userdata()
// will return NULL if empty, not a boolean.
}

Resources