• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 10508933565

22 Aug 2024 01:20PM UTC coverage: 77.706% (-0.2%) from 77.868%
10508933565

push

github

web-flow
Merge pull request #139 from RonasIT/feat/update-dependencies

Update dependencies

9 of 16 new or added lines in 8 files covered. (56.25%)

1 existing line in 1 file now uncovered.

962 of 1238 relevant lines covered (77.71%)

11.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Traits/MigrationTrait.php
1
<?php
2

3
namespace RonasIT\Support\Traits;
4

5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\Schema;
7
use Illuminate\Database\Schema\Blueprint;
8

9
trait MigrationTrait
10
{
11
    public function addForeignKey($fromEntity, $toEntity, $needAddField = false, $onDelete = 'cascade')
12
    {
13
        Schema::table(
×
14
            $this->getTableName($fromEntity),
×
15
            function (Blueprint $table) use ($toEntity, $needAddField, $onDelete) {
×
16
                $fieldName = Str::snake($toEntity) . '_id';
×
17

18
                if ($needAddField) {
×
19
                    $table->unsignedInteger($fieldName);
×
20
                }
21

22
                $table
×
23
                    ->foreign($fieldName)
×
24
                    ->references('id')
×
25
                    ->on($this->getTableName($toEntity))
×
26
                    ->onDelete($onDelete);
×
NEW
27
            }
×
NEW
28
        );
×
29
    }
30

31
    public function dropForeignKey($fromEntity, $toEntity, $needDropField = false)
32
    {
33
        $field = Str::snake($toEntity) . '_id';
×
34
        $table = $this->getTableName($fromEntity);
×
35

36
        if (Schema::hasColumn($table, $field)) {
×
37
            Schema::table($table, function (Blueprint $table) use ($field, $needDropField) {
×
38
                $table->dropForeign([$field]);
×
39

40
                if ($needDropField) {
×
41
                    $table->dropColumn([$field]);
×
42
                }
43
            });
×
44
        }
45
    }
46

47
    public function createBridgeTable($fromEntity, $toEntity)
48
    {
49
        $bridgeTableName = $this->getBridgeTable($fromEntity, $toEntity);
×
50

51
        Schema::create($bridgeTableName, function (Blueprint $table) {
×
52
            $table->increments('id');
×
53
        });
×
54

55
        $this->addForeignKey($bridgeTableName, $fromEntity, true);
×
56
        $this->addForeignKey($bridgeTableName, $toEntity, true);
×
57
    }
58

59
    public function dropBridgeTable($fromEntity, $toEntity)
60
    {
61
        $bridgeTableName = $this->getBridgeTable($fromEntity, $toEntity);
×
62

63
        $this->dropForeignKey($bridgeTableName, $fromEntity, true);
×
64
        $this->dropForeignKey($bridgeTableName, $toEntity, true);
×
65

66
        Schema::drop($bridgeTableName);
×
67
    }
68

69
    protected function getBridgeTable($fromEntity, $toEntity)
70
    {
71
        $entities = [Str::snake($fromEntity), Str::snake($toEntity)];
×
72
        sort($entities, SORT_STRING);
×
73

74
        return implode('_', $entities);
×
75
    }
76

77
    protected function getTableName($entityName)
78
    {
79
        if (Schema::hasTable($entityName)) {
×
80
            return $entityName;
×
81
        }
82

83
        $entityName = Str::snake($entityName);
×
84

85
        return Str::plural($entityName);
×
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc