Skip to content

Commit

Permalink
Update BaseModel.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jan 26, 2025
1 parent d0fa90f commit d2dbda3
Showing 1 changed file with 0 additions and 54 deletions.
54 changes: 0 additions & 54 deletions src/Abstract/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,60 +60,6 @@ public function getHashedKey(string|null $field = null): mixed
return $this->getAttribute($field);
}

/**
* Checks if the model is owned by the $owner model.
*
* It tries to guess the relation by the name of the $owner model.
* e.g., if the $owner model is called User, then the first guess would be `user()`.
* If the `user()` relation does not exist, then it tries to use the plural version `users()`.
* Else it throws an exception.
*
* If the relation name is different, you can pass it as the second parameter.
*/
public function isOwnedBy(Model $owner, string|null $relation = null): bool
{
return $this->owns($owner, $relation);
}

/**
* Checks if the model is the owner of the $ownable model.
*
* It tries to guess the relation by the name of the $ownable model.
* e.g. if the $ownable model is called Post, then the first guess would be `post()`.
* If the `post()` relation does not exist, then it tries to use the plural version `posts()`.
* Else it throws an exception.
*
* If the relation name is different, you can pass it as the second parameter.
*/
public function owns(Model $ownable, string|null $relation = null): bool
{
if ($relation) {
return !is_null($this->$relation()->find($ownable));
}

$relation = $this->guessSingularRelationshipName($ownable);
if (method_exists($this, $relation)) {
return !is_null($this->$relation()->find($ownable));
}

$relation = $this->guessPluralRelationshipName($ownable);
if (method_exists($this, $relation)) {
return !is_null($this->$relation()->find($ownable));
}

throw new \InvalidArgumentException('No relationship found. Please pass the relationship name as the second parameter.');
}

public function guessSingularRelationshipName(Model $ownable): string
{
return Str::camel(class_basename($ownable));
}

public function guessPluralRelationshipName(Model $ownable): string
{
return Str::plural(Str::camel(class_basename($ownable)));
}

/**
* Retrieve the model for a bound value.
*
Expand Down

0 comments on commit d2dbda3

Please sign in to comment.