Having problems with filling inputs with values i get from a query.
$fieldData = myModel::where('id', '=', '5')->first();
//Here's not working properly :
Input::merge(array('inputName' => $fieldData->name));
$variableBag = \Input::all();
and the inputName field's value is being transferred with json.
This one works instead of above :
//This one is working
Input::merge(array('inputName' => 'manual Value something something here'));
Thanks in advance.
---UPDATE---
The problem is the value is being returned is null. I've written a test and solved it. Thank you for your help.
$fieldData = myModel::where('id', '=', '5')->first();
//change to this
$input_all = array_merge(Input::all(), array('inputName' => $fieldData->name));
$variableBag = $input_all;
I hope this help, don't forget to check the result of $fieldData , is empty or not...
Check your variable $fieldData.
And use instead first this: firstOrFail.
Related
Normally data retrieved as an object from database in Laravel when we use get() function. But I want to retrieved data as an array.
In CodeIgnitor we use get_array(). What we use in Laravel ?
I have already tried with toArray(). But no result.
$doctor_result = Doctor::select('*')->toArray();
How to solve that?
I got result. just change the code
$doctor_result = Doctor::all()->toArray();
You can also do like this.
$result = DB::table('tableName')->get();
$resultArray = $result->toArray();
second way, but some tricky.
$resultArray = json_decode(json_encode($result), true);
I wanted to save the data in this format into database option field:
{"type":"type_value"}
I am getting data in post varaibles like this
$type_value = $request->input('type_value');
$type = $request->input('type');
How Can I save this in database?
I have tried this
$data['options'] = array($type,$type_value);
But by this it is saving in this format:
["Qualifiers","1"]
I even tried doing this:
$data['options'] = json_encode(array($type,$type_value));
Instead it is saving like this
"[\"Qualifiers\",\"1\"]"
how can I do this?
You just have to change your array definition. Your array considers 2 different elements i.e type and type_value. So just make your array with key value pair and you are all set.
json_encode(array($type => $type_value))
Check this :- Fiddle
I have retrieved some data from db using:
$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get();
and I would like to insert what I have retrieved in another table like:
DB::table('tmp_other')->insert($filtered);
but I receive thie error:
Type error: Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, object given
which is the best way to do this?
The SQL way to do blunk inserts of this is something more native like :
DB::insert(
"INSERT INTO tmp_other (type,description,note)
SELECT DISTINCT type,description,note FROM tmp_table"
);
This will avoid the whole transfer to webserver/transfer back to the SQL server process.
this will do the trick for you try this :)
$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get();
$filtered->toArray();
DB::table('tmp_other')->insert($filtered);
This will do the trick :)
$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get()->toArray();
$to_fill = [];
foreach($filtered as $record) {
$to_fill[] = (array)$record;
}
DB::table('tmp_other')->insert($to_fill);
You can do it one line, like this:
Region::insert($tempCollection->toArray());
I have found that:
Region::insert($tempCollection->toArray());
Doesn't work when you have appends in the model. This does not happen if you use getAttributes on each model. Therefore, this should be safe:
Region::insert($tempCollection->map(fn($x) => $x->getAttributes())->toArray()
I am little bit struggling in setting array in session.
Here is my code:-
Controller:
function taketest(){
$this->load->model('', '');
$questions_for_test = $this->session->userdata('questions_for_test');
$difficulty_level = $this->session->userdata('difficulty_level');
$question_id = $this->session->userdata('question_id');
if($question_id==""){
$question_id = $this->myjcat->returnRandomQuestion($questions_for_test,$difficulty_level);
$this->session->set_userdata('question_id',$question_id);
}
$question_details = $this->myjcat->getQuestion($question_id);
}
Model:
function returnRandomQuestion($questions_for_test, $difficulty_level){
$question_id = array_rand($questions_for_test[$difficulty_level], 1);
$used_question=array();
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
return $questions_for_test[$difficulty_level][$question_id];
}
But when I call:
$used_questions = $this->session->userdata('used_questions');
in controller
in the controller it will not return me an array.It gives me last value stored in it.
I could be misreading things, but it looks like you are only storing one value.
// this code:
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
// is the same as this code
$this->session->set_userdata('used_questions',$questions_for_test[$difficulty_level][$question_id]);
You're probably looking for this:
// fetch the stored copy first.
$used_questions = $this->session->userdata('used_questions');
if(!is_array($used_questions)) $used_questions = array();
// note the []
$used_questions[] = $questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
You can set array values in session data like this :-
$this->session->set_userdata('used_questions', json_encode($used_questions));
And retrieve the data as :-
json_decode($this->session->userdata('used_questions'));
If you want the retrieved array data as associative array :-
json_decode($this->session->userdata('used_questions'), true);
Hope it hepls you :)
The problem is that $used_questions is storing the value stored in $questions_for_test[$difficulty_level][$question_id] and not the array.
So do this $this->session->set_userdata('used_questions',$questions_for_test);
This is because the data you pass to the $used_questions is a value.
You might want to do something like this:
array_push($used_questions, $questions_for_test[$difficulty_level][$question_id]);
*appending / adding a new value to an array
I am trying to post serialize value to an array, is it possible.. check following...
$product = serialize($_POST['product']);
$values = array('name'=>$_POST['name'], 'product'=>$product);
print_r($values);
please let me know your suggestion to achieve this. thanks.
i solved the problem, i have used '' instead of "" for serialized data and it worked. thanks.