I am not getting all the key values of and array using foreach, its giving only first key value.
$order_id = DB::table('order')->where('delivery_boy_id', $delivery_boy_id)->where('is_accept', 1)->whereRaw('delivery_completed_at < time_of_assignment')->get();
When i run this code i get two key value
array:2 [0 => {#325
+"id": 37
+"order_id": 8261
+"delivery_boy_id": 8}1 => {#326
+"id": 38
+"order_id": 8261
+"delivery_boy_id": 8]
After using this json response i am getting the response of only one key value
foreach($order_id as $value){ $values = $value) };
$this->response['items'] = $order_id;
return json_encode($this->response);
check this
$order_id = DB::table('order')->where('delivery_boy_id', $delivery_boy_id)->where('is_accept', 1)->whereRaw('delivery_completed_at < time_of_assignment')->get();
// return $order_id;(returns json array)
$data = [];
foreach($order_id as $order)
{
var_dump($order);
//in first loop first array will come
//second loop second array
$data[]=[
'id' => $order['id'],
'..' => '..'
];
}
return $data;
}
Dd means dump and die so it stops execution. If you dd in a for each loop, you will only get the current value in the loop (I.e the first one) because it will halt execution and you will never reach the second loop through. Use var_dump to allow execution to continue and dump variables.
If you simply want to return the Json to the user, you can just return the result of your query as laravel handles serialisation for you. You don't need to loop over the result. See the docs:
https://laravel.com/docs/5.1/eloquent-serialization
You are overwriting $values in your loop. So every time it passes through the for each loop, it overwrites $values with the current $value so it will only ever contain 1 object. You don't need to do the serialisation manually, use the laravel helper methods
Also in your code snippet you are looping over $order, but the result of your query is assigned to $order_id? I'm assuming that's due to abbreviating your code
Related
I have the following code:
$datas = $request->all();
if (!empty($datas)){
for ($i=1; $i<count($datas); $i++){
$value = [
'questionnaire_id' => $datas[$i]->questionnaires_id,
'question_id' => $datas[$i]->id,
'answer' => $datas[$i]->key
];
return $value;
}
}
I am getting error:
Undefined offset: 1
With input form:
<input type="" name="{{$question->id}}" value="{{$key}}">
How can I receive this value in controller?
You're assuming that $datas is a numerical array starting at index 1 with no gaps. That may not be the best assumption.
Based on my knowledge of request()->all(), it's going to return an associative array of all your user input so you wouldn't be able to access an $i key on $datas. Just because it may have 1 element, doesn't mean that index on the array will be 1.
Perhaps you want to retrieve a specific user input, for example, if you had a checkbox named checkbox, you may want to use request()->input('checkbox') instead of request()->all(), but I'd still assume your array keys would start from 0 and not 1.
When all else fails, set a breakpoint and use a debugger to see the value of $datas. If you don't have a debugger (which I highly recommend) you can use dd($datas); to die and dump the value.
You could try initialize the variable i = 0. Also make a dd($datas) to see what you are evaluating.
$datas = $request->all();
if (!empty($datas)){
for ($i=0; $i<count($datas); $i++){
$value = [
'questionnaire_id' =$datas[$i]->questionnaires_id,
'question_id' =$datas[$i]->id,
'answer' =$datas[$i]->key
];
return $value;
}
}
Basiclly I have an url that have a parameter.
So, I need to access those parameter using GET. This is the GET that coming:
Array
(
[r] => hanwa/incoming-pipe/assign-incoming
[1] => Array
(
[min_urut] => 1
[max_urut] => 44
)
[_] => 1496645704980
)
In docs, docs,
We can use like this :
echo $request->get('min_urut');
But, I got nothing. Please Advise.
Use Concept of ArrayHelper class :-> http://www.yiiframework.com/doc-2.0/guide-helper-array.html#getting-values
ex:
$data = ArrayHelper::getValue($request->get(), 'Temp.yourvalue.yourindex');
Main use of $request->get() method mainly returns to you a value from $_GET,
so example is
$temp = $request->get('Temp'); // Here $temp variable contains $_GET['Temp']
$data = $temp['yourvaluename'][0];
Judging by the fact that you have an array with the key [r] you have an associative array, method get() you will not get it, you need to do it just like this ..
echo $request->get()[1]['min_urut'];
My question is the exact same as How to Set a Value to NULL when using Zend_Db
However, the solution given in that question is not working for me. My code looks like the following. I call updateOper on the Model class when update is clicked on the front end. Inside updateOper, I call another function trimData() where I first trim all whitespace and then I also check that if some of the fields are coming in empty or '' I want to set them to default values or NULL values. Therefore I am using new Zend_db_expr('null') and new Zend_db_expr('default') .
The code is as follows:
private function trimData(&$data ) {
//Trim whitespace characters from incoming data.
foreach($data as $key => $val)
{
$data[$key] = trim($val);
if($data['notes'] == '') {
error_log("set notes to null/default value");
$data['notes'] = new Zend_db_expr('DEFAULT');
}
}
}
public function updateOper($data, $id)
{
$result = 0;
$tData = $this->trimData($data);
error_log("going to add data as ".print_r($data, true));
$where = $this->getAdapter()->quoteInto('id = ?', $id);
$result = $this->update($data, $where);
return $result;
}
The error_log statement prints the $data array as follows:
[id] => 10
[name] => alpha
[notes] => DEFAULT
As a result, the notes column has value ='DEFAULT' instead of picking the default value given in the table definition.
I have been trying to figure out what is wrong, but have not been able to find a solution.
I would really appreciate your help.
Thanks so much!
Your $data['notes'] is being changed to the __toString() value of the Zend_Db_Expr instead of preserving the actual object.
Maybe the reference is clogging things up. Else you may need to move the expression declaration into the actual update query.
How can I return a value of 'no data found' if my query returns no results???
This is my code:
function getTerms($letter) {
$this->db->select('term, definition');
$this->db->from('glossary');
$this->db->where(array('letter' => $letter));
$query = $this->db->get();
foreach ($query->result() as $row) {
$data[] = array(
'term' => $row->term,
'definition' => $row->definition
);
}
return $data;
}
It currently returns the $data variable even if the query returns no results which is giving me php errors. How can I check that there are results before returning the $data array.
Simply check that the query returns at least one row:
if ($query->num_rows() > 0) {
// query returned results
} else {
// query returned no results
}
Read more in the docs
It currently returns the $data variable even if the query returns no results which is giving me php errors.
It's a good habit to initialize the array that you intend to build:
$data = array();
// Loop through possible results, adding to $data
If there are no results you'll get an empty array returned, then check that in your controller.
This way, at least the variable is defined and you won't get notices.
What about to give initial empty array value for $data
Just put
$data = array();
before foreach
<?php
return is_array($data) ? $data : array();
}
Soooo....long time reader first time poster. For the first time, can't find the answer!
I've got a model that returns an array of results (working). Then I loop through the results with a foreach and append the array with additional values from a second function in my model. Problem is that only the first of my several function calls return any results. The function get_offering_total() only returns the value for the first listed call (cash). This is returning the correct value, but any subsequent attempts to use the same function returns nothing. A var_dump shows that the array is structured correctly on the output, but the values are all null. Is there an issue with calling the same model method with different parameters? Something needs to be cleared/reset?
Here's the code...
//$data['offerings'] stores the results from my first query
foreach( $data['offerings'] as $key => $value ) {
//Set filters
$cash_filter = array( 'method' => '5' );
$checks_filter = array( 'method' => '6' );
//Get totals
$data['offerings'][$key]['cash'] =
$this->model_records->get_offering_total( $value['offering_id'], $cash_filter);
$data['offerings'][$key]['checks'] =
$this->model_records->get_offering_total( $value['offering_id'], $checks_filter);
...other stuff...
}
Thanks for your help!
Here's a simple test to see if all the code you wrote is correct. Surround everything in your function (in the model) within a try/catch block, and see if it throws any kind of exception. You'd be surprised how many exceptions get thrown but PHP doesn't show them because you have error reporting off in your PHP config. You can do the same for the two lines on which you call the method in your controller and see if they are all good.
PHP Exceptions Reference