I\'m needing to convert this query for Laravel and I\'m having issues trying to create an Insert... Select statement using Laravel\'s Eloquont ORM, or Queries. I\'m not sure
There is no way of doing this in one query (unless you are on Laravel 5.7), however I came across the same issue and wanted to make sure I can keep using a certain select I build with the QueryBuilder.
So what you could do, to keep things half what clean and to reuse functionality which has built a select statement before, is this:
/**
* Wherever your Select may come from
**/
$select = User::where(...)
->where(...)
->whereIn(...)
->select(array('email','moneyOwing'));
/**
* get the binding parameters
**/
$bindings = $select->getBindings();
/**
* now go down to the "Network Layer"
* and do a hard coded select, Laravel is a little
* stupid here
*/
$insertQuery = 'INSERT into user_debt_collection (email,dinero) '
. $select->toSql();
\DB::insert($insertQuery, $bindings);
UPDATE Laravel 5.7
As of Laravel 5.7.17 you can use ->insertUsing(). See here for details. Thank you @Soulriser for pointing this out.
So above query would look like this:
DB::table('user_debt_collection')->insertUsing(['email','dinero'], $select);