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()
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 have an array like:
$expl = Array([0] => 51465, [1] => 411002);
I want to get all records that contain zip codes like 51465 or 411002. To get this I tried:
foreach ($expl as $exp) {
$this->datatables->or_like('vendor.zip', $exp);
}
But this is showing me the following error:
Call to undefined method Datatables::or_like()
Can someone please tell me the solution or any alternative way for this?
Please use database Codeigniter object db or you can use custom datatables.
I would suggest you please use db object.
$this->db->or_like('vendor.zip', $exp);
But you should also use "IN" query.
$names = array(51465, 411002);
$this->db->where_in('vendor.zip', $names); // Here vendor is alias of table name
// Produces: WHERE vendor.zip IN (51465, 411002)
I have inserted values into database for multiple selection like this somevalue1, somevalue2, somevalue3 in Laravel using implode function.
Controller.php
$project_name=$request->project_name;
$project_id=$request->project_id;
$client_id=$request->client_id;
$project_status=$request->project_status;
$approver = implode(",",Input::get(array('approver')));
I want value in separately not like somevalue1, somevalue2. How can I solved this?
You can use json_encode during saving to database.
$save_approver = json_encode($request->approver);
and json_decode when retrieving from database.
$get_approver = json_decode($your_variable, TRUE);
foreach($get_approver as $value){
// operations
}
I have a table that is listed like this:
Table name: test
|--------------------|
|------Column 1------|
|--------------------|
|John,Raul,Matt,Tyler|
|Tim,Hannah----------|
|Zeke,Brady,Tim,Tyler|
|Elliot,Zeke---------|
|Matt,Andrew,Idda----|
...
And I need to get all of those names into a PHP array, without multiple cases of the same name.
Would I be better off getting each unique name via SQL, or should I just pull the column, and then parse through it via PHP?
The final PHP array would look like:
$test_array[0]="John"
$test_array[1]="Raul"
$test_array[2]="Matt"
$test_array[3]="Tyler"
$test_array[4]="Tim"
$test_array[5]="Hannah"
$test_array[6]="Zeke"
$test_array[7]="Brady"
$test_array[8]="Elliot"
$test_array[9]="Andrew"
$test_array[10]="Idda"
I'm not sure how I would parse each cell in the table. What would you guys do?
I agree it's best to normalize the database. However, given the situation you've described, I would perform the SQL select and use PHP to run through the results.
I'd initialize $results as an empty string:
$results = '';
Then I'd concatenate each row with $results:
$results .= $row;
Then I'd use explode() to put all of the names in an array:
$all_names = explode(',', $results);
Then I'd use array_unique() to reduces the $all_names array to unique values:
$unique_names = array_unique($all_names);
HTH.
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.