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

RonasIT / laravel-helpers / 12489544920

25 Dec 2024 04:53AM UTC coverage: 78.401% (+0.02%) from 78.385%
12489544920

Pull #168

github

web-flow
Merge a36445178 into 338a94856
Pull Request #168: #140 global export mode not affect for email testing

2 of 2 new or added lines in 1 file covered. (100.0%)

1020 of 1301 relevant lines covered (78.4%)

11.92 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);
×
27
            }
×
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