Skip to content

Instantly share code, notes, and snippets.

@Mevrael
Last active January 28, 2016 15:15
Show Gist options
  • Save Mevrael/efcc0dca21a6da3ee6da to your computer and use it in GitHub Desktop.
Save Mevrael/efcc0dca21a6da3ee6da to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\Traits;
use Illuminate\Support\Facades\DB;
use PDO;
trait Iterable
{
public static function iterate(array $columns = [])
{
$query = DB::table((new self)->getTable());
if (!empty($columns)) {
$query->select($columns);
}
$pdo = $query->getConnection()->getPdo();
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$statement = $pdo->prepare($query->toSql());
$statement->execute($query->getBindings());
while ($row = $statement->fetch(PDO::FETCH_OBJ)) {
yield $row;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment