Skip to content

Commit c08a05f

Browse files
committed
refactor
1 parent 2e40f25 commit c08a05f

1 file changed

Lines changed: 5 additions & 28 deletions

File tree

src/Relations/Relation.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,7 @@ abstract class Relation
2727
/**
2828
* The related model instance
2929
*/
30-
public Model $model;
31-
32-
/**
33-
* The primary key on the parent model
34-
*/
35-
public string $primaryKey;
36-
37-
/**
38-
* The foreign key on the related model
39-
*/
40-
public string $foreignKey;
30+
protected Model $model;
4131

4232
/**
4333
* Optional query callback for custom constraints
@@ -76,27 +66,14 @@ abstract class Relation
7666
* @param string|null $primaryKey Optional primary key override
7767
*/
7868
public function __construct(
79-
public Model $parentModel,
69+
protected Model $parentModel,
8070
string $relatedModelClass,
81-
?string $foreignKey = null,
82-
?string $primaryKey = null,
71+
protected ?string $foreignKey = null,
72+
protected ?string $primaryKey = null,
8373
) {
8474
$this->model = model($relatedModelClass);
8575
$this->primaryKey = $primaryKey ?? get_model_property($this->parentModel, 'primaryKey');
86-
$this->foreignKey = $foreignKey ?? $this->guessForeignKey();
87-
}
88-
89-
/**
90-
* Guess the foreign key name based on convention
91-
*
92-
* Uses the parent model's table name (singular) and primary key
93-
* to generate a foreign key following the pattern: {singular_table}_{primary_key}
94-
*
95-
* @return string The guessed foreign key name
96-
*/
97-
protected function guessForeignKey(): string
98-
{
99-
return get_foreign_key($this->parentModel);
76+
$this->foreignKey = $foreignKey ?? get_foreign_key($this->parentModel);
10077
}
10178

10279
/**

0 commit comments

Comments
 (0)