Laravel how many rows inserted in an insert query?

Naser Nikzad

I have this query eloquent in Laravel, I was curious to know. If there is a way to know how many records inserted or how many records ignored to the table?

DB::table('mytable')->insertOrIgnore($data)

Note: One of the manual ways can be, counting records of table before and after process. but this has performance effect, if there be any better way achieving this.

mrhn

The function insertOrIgnore() returns the affected rows.

/**
 * Insert a new record into the database while ignoring errors.
 *
 * @param  array  $values
 * @return int
 */
public function insertOrIgnore(array $values) {

So you can simply used the affected and compare to to what was expected to be inserted.

$affected = DB::table('mytable')->insertOrIgnore($data);
$ignored = count($data) - $affected;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel 4.2 count how many rows are inserted today

How to set the exact number of rows that should be inserted in an "insert ignore ... select ..." query?

Returning Index of inserted rows in a multiple row insert query in Postgres

How do I get a count on inserted rows for INSERT query with nested SELECT? No luck with affected_rows, result_id, num_rows

How to get inserted IDs of multiple rows on one INSERT?

Python pandas how many rows insert into DB

How insert to MySQL many rows PHP

How to use inserted query id for running another query in Laravel

How to get the data of the inserted multiple rows in Laravel Eloquent ORM

Is it possible to match old and new rows inserted by INSERT ... SELECT query by primary key

How to fix a query that produces too many rows?

Insert many rows in Gnumeric

Insert many rows with Dapper

How to insert a one to many relationship in Laravel

how to insert one to many relation in laravel?

In pandas Dataframe, how do you insert rows, then copy/paste the cell values to the inserted rows?

How to select many rows from 1 table and insert into a specific JSONB field of a specific row in another table? But in a single raw SQL query

Laravel query many to Many

How to insert pivot table in case of many to many relation? (Laravel 5.3)

Laravel Many to Many, how to insert/get data inside the pivot table?

How to insert multiple rows in laravel, need explanation

How to insert multiple rows in laravel 5?

Laravel - How to INSERT rows with Reserved Words and dashes

How many rows to insert per notification in a notification system?

How to use python mysqldb to insert many rows at once

How to use python mysqldb to insert many rows at once

How to select all inserted rows to execute an insert trigger with a stored procedure in postgresql?

How to count how many records got inserted within 5 minutes from now in a single query?

MySQL INSERT query works but Data is not inserted