Batch insert in Laravel 5.2 - laravel

I am using a API's with lot's of calculation almost 100 database fields at the end with a big Foreach loop.
In every iteration i insert data in database. I want to insert data in once at the end (Batch Insert like in CodeIgniter).
Any body have idea how to insert all data at the end of iteration. instead of every iteration it insert row in database.
I want to insert data at the end of loop. Any help or idea appreciated.

Use insert() method for bulk insertion. First, build an array with this structure:
$data = [
['name' => 'John', 'age' => 25],
['name' => 'Maria', 'age' => 31],
['name' => 'Julia', 'age' => 55],
];
Then insert the data using Eloquent model:
Model::insert($data);
Or using query builder:
DB::table('table_name')->insert($data);

Related

I want the data obtained from find table1 to insert into table2 in laravel

I want the data obtained from find table1 to insert into table2
$table1= Table1::find($id);
$table2= new Table2();
without
$table2->field1 = $table1->field1;
$table2->field2 = $table1->field2;
where I have multiple fields
How do I use it? to be able to write short code
If all the fields are same in both tables, you can try,
$table1 = Table1::find($id);
// Since $table1 is an object so you have to convert it into array bcs create accepts only array
$table1 = $table1->toArray();
// Also you will have id as autoincrement in table2 so you have to remove id of record of table1 before inserting to table2.
unset($table1['id']);
//Now insert array into table2
Table2::create($table1);
Laravel has the replicate() function that will do your job.
This is how you can use it:
$table1 = Table1::find($id);
$table2 = $table1->replicate();
$table2 = $table2->toArray();
Table2::create($table2);
This function will clone your one model data into another object. If you want to change/add/replace any data from the model data then you can also add/modify it:
$table1 = Table1::find($id);
$table2 = $table1->replicate()->fill([
'field_name' => 'value' // Override any data
'new_field_name' => 'value' // Add new field data
]);
$table2 = $table2->toArray();
Table2::create($table2);
I hope that this is what you are looking for. 😄😄
mean like this?
$table1 = Table1::find($id);
Table2::create([
'field1' => $table1->field1,
'field2' => $table1->field2,
]);
if the fields in table2 are same with table1
$table1 = Table1::find($id);
Table2::create($table1->toArray());

Laravel query builder - bulk insert - Ignore columns not found

I am using laravel query builder where I am bulk inserting hundreds of rows. I want to insert the column found and ignore the columns not found in table
Here is my code:
$proposalOpsEvents = DB::table('table1')->where('proposal_id', $proposal->id)->get();
$proposalOpsEvents = $proposalOpsEvents->toArray();
DB::connection('mysql_archive')->table('archive_table1')->insert($proposalOpsEvents);
I get error "Unknown column".
In table 1 new columns are getting added dynamically. I want to ignore the newly added columns when inserting in archive_table1.
For example,
DB::table('archive_table1')->insert([
'email' => 'abc#example.com', //email column found - insert
'phone' => 0, // phone column found - insert
'address' => 'A'// address column (newly added) not found - ignore
]);
Any solution for these?
Have you tried the insertOrIgnore method?
DB::table('archive_table1')->insertOrIgnore([
'email' => 'abc#example.com', //email column found - insert
'phone' => 0, // phone column found - insert
'address' => 'A'// address column (newly added) not found - ignore
]);
You can use the DB::squemaBuilder
$columns=DB::connection('your_connection')->getSchemaBuilder()->getColumnListing('table_name');
This will return an array with all columns names of the table and with array manipulation you can intersect your columns to insert with columns in table.
$newData=[
'email' => 'abc#example.com', //email column found - insert
'phone' => 0, // phone column found - insert
'address' => 'A'// address column (newly added) not found - ignore
];
$newDataClean=array_intersect_key($newData, array_flip($columns));

DBMS_STATS with Looping

I just tried to learn new things. I just make a experiment to analyze tables with looping. But, I found error that said
'Invalid reference to variable 'indx'
. If I take out the DBMS_STATS 'thing' and just to print the result, it works. But when I tried to analyze the tables, the error came out.
CREATE OR REPLACE PROCEDURE ANALYZE_TABLE
AS
CURSOR table_cur IS
SELECT TABLE_NAME
FROM ALL_TABLES
WHERE TABLE_NAME LIKE 'STUDENT%';
table_nm table_cur%ROWTYPE;
TYPE table_nms IS TABLE OF table_nm%TYPE;
l_table table_nms;
BEGIN
OPEN table_cur;
FETCH table_cur BULK COLLECT INTO l_table;
CLOSE table_cur;
FOR indx IN 1..l_table.COUNT LOOP
IF (indx.table_name = 'STUDENT_DETAILS') THEN
dbms_stats.gather_table_stats(ownname => 'ADMIN', tabname => indx.table_name , estimate_percent => 100,
method_opt => 'for all indexed columns size auto',
degree => 4 ,cascade => TRUE );
ELSIF (indx.table_name = 'STUDENT_ALLOWANCE' OR indx.table_name = 'STUDENT_PAYMENT')
THEN
DBMS_STATS.GATHER_TABLE_STATS (ownname => 'ADMIN', tabname => indx.table_name , estimate_percent => 100,
method_opt => 'for all indexed columns size auto',
degree => 4 ,cascade => TRUE );
ELSE
DBMS_STATS.GATHER_TABLE_STATS (ownname => 'ADMIN', tabname => indx.table_name , estimate_percent => 100,
method_opt => 'for all indexed columns size auto',
degree => 4 ,cascade => TRUE );
END IF;
--DBMS_OUTPUT.PUT_LINE(l_table(indx).TABLE_NAME);
END LOOP;
END ANALYZE_TABLE;
Any suggestion? Or a better way to analyze tables with this loop?
Thank you in advance for anyone that helping me. :)
You need to reference the contents of your array, not the loop index. The loop index tells you which element in the array. So:
IF (l_table(indx).table_name = 'STUDENT_DETAILS') THEN
and so on
You should reference the values in your table as follows:
if (l_table(indx).table_name = 'STUDENT_DETAILS') ...

Oracle markup into temporary table

I'm using ctx_doc.markup to highlight search results and insert them into a temporary table. Then i retrieve the results from the temporary table. All is running in one transaction. However, the results get deleted from the temporary table (or never inserted?) before i can retrieve them. If i use a normal table it's working fine. Here's the query i'm using:
BEGIN
FOR cur_rec IN (SELECT id FROM contents WHERE CONTAINS(text, 'test', 1) > 0)
LOOP
CTX_DOC.markup(
index_name => 'I_CONTENTS_TEXT',
textkey => TO_CHAR(cur_rec.id),
text_query => 'test',
restab => 'CONTENTS_MARKUP',
query_id => cur_rec.id,
plaintext => FALSE,
tagset => 'HTML_NAVIGATE');
END LOOP;
END;
EOF

Laravel 4: insert with max+1 in laravel

I want to run the following query
INSERT INTO static_pages (`page_name`, `title`, `page_display_order`) SELECT 'test','test', MAX(page_display_order)+1 FROM static_pages;
How to execute this query with laravel eloquent? Is that possible?
Update
Thanks #Joseph Silber
I used this in elaquant as like
$staticpage->page_display_order = DB::raw("($subquery)");
$staticpage->save();
That helped to get the last inserted id.
Use DB::raw() with a subquery:
$subquery = DB::table((new StaticPage)->getTable() . ' as page_alias')
->selectRaw('page_display_order + 1 as pdo')
->orderBy('page_display_order', 'desc')
->take(1)->toSql();
StaticPage::create([
'page_name' => 'test',
'title' => 'test',
'page_display_order' => DB::raw("($subquery)"),
]);

Resources