-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from weblabormx/feature-seeds-by-table
save seeds by tables
- Loading branch information
Showing
5 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Elimuswift\DbExporter; | ||
|
||
trait SeederHelper | ||
{ | ||
|
||
public function saveData($table, $item) | ||
{ | ||
// If have primary id | ||
if(isset($item['id'])) | ||
{ | ||
if(\DB::table($table)->where('id', $item['id'])->count() > 0) | ||
{ | ||
\DB::table($table)->where('id', $item['id'])->update($item); | ||
} | ||
else | ||
{ | ||
\DB::table($table)->insert($item); | ||
} | ||
} | ||
else | ||
{ | ||
$ids = collect($item)->filter(function($item, $key) { | ||
return str_contains($key, '_id'); | ||
})->keys()->values(); | ||
|
||
// If there isnt any column with _id, so check that every column matches | ||
if($ids->count() <= 0) { | ||
$ids = collect($item)->keys()->values(); | ||
} | ||
$object = \DB::table($table); | ||
foreach ($ids as $id) { | ||
$object = $object->where($id, $item[$id]); | ||
} | ||
|
||
// save or update | ||
if($object->count() > 0) | ||
{ | ||
$object->update($item); | ||
} | ||
else | ||
{ | ||
$object->insert($item); | ||
} | ||
|
||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Seeder; | ||
use Elimuswift\DbExporter\SeederHelper; | ||
|
||
class {{className}} extends Seeder { | ||
class {{className}} extends Seeder | ||
{ | ||
|
||
use SeederHelper; | ||
|
||
public function run() | ||
{ | ||
DB::statement('SET FOREIGN_KEY_CHECKS=0'); | ||
|
||
{{run}} | ||
{{run}} | ||
} | ||
|
||
} |