Last active
January 28, 2016 15:15
-
-
Save Mevrael/efcc0dca21a6da3ee6da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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