|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reedware\LaravelBlueprints\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
|
6
|
|
|
|
|
7
|
|
|
trait ForeignKeys |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Drops the specified Foreign Key if it exists. |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $index |
|
13
|
|
|
* @param \Illuminate\Database\Connection $connection |
|
14
|
|
|
* |
|
15
|
|
|
* @return \Illuminate\Support\Fluent|null |
|
16
|
|
|
*/ |
|
17
|
|
|
public function dropForeignIfExists($index, Connection $connection = null) |
|
18
|
|
|
{ |
|
19
|
|
|
// Drop the Foreign Key if it exists |
|
20
|
|
|
if($this->foreignExists($index, $connection)) { |
|
21
|
|
|
return $this->dropForeign($index); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// Return NULL |
|
25
|
|
|
return null; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns whether or not the specified Foreign Key exists. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $index |
|
32
|
|
|
* @param \Illuminate\Database\Connection $connection |
|
33
|
|
|
* |
|
34
|
|
|
* @return boolean |
|
35
|
|
|
*/ |
|
36
|
|
|
public function foreignExists($index, Connection $connection = null) |
|
37
|
|
|
{ |
|
38
|
|
|
// Determine the Foreign Keys |
|
39
|
|
|
$foreigns = $this->getForeignKeyNames($connection); |
|
40
|
|
|
|
|
41
|
|
|
// Return the the Index is in the list of Foreigns |
|
42
|
|
|
return in_array($index, $foreigns); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns the Foreign Key names. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Illuminate\Database\Connection $connection |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getForeignKeyNames(Connection $connection = null) |
|
53
|
|
|
{ |
|
54
|
|
|
// Determine the Foreign Keys |
|
55
|
|
|
$foreigns = $this->getForeignKeys($connection); |
|
56
|
|
|
|
|
57
|
|
|
// Convert them to their Names |
|
58
|
|
|
return array_map(function($foreign) { |
|
59
|
|
|
return $foreign->getName(); |
|
60
|
|
|
}, $foreigns); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the Foreign Keys for this Table. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Illuminate\Database\Connection|null $connection |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getForeignKeys(Connection $connection = null) |
|
71
|
|
|
{ |
|
72
|
|
|
// Determine the Schema Manager |
|
73
|
|
|
$manager = $this->getSchemaManager($connection); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
// Return the Foreign Keys |
|
76
|
|
|
return $manager->listTableForeignKeys($this->table); |
|
77
|
|
|
} |
|
78
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.