Skip to content

Commit

Permalink
relations renamed.
Browse files Browse the repository at this point in the history
HasManyWithColumnKeyArray to hasManyArrayColumn,
BelongsToManyWithManyKeys to belongsToManyKeys and
HasManyWithManyKeys to hasManyKeys.
  • Loading branch information
MrPunyapal committed Apr 3, 2023
1 parent 360d701 commit ebbddd8
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 194 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class Post extends Model {

```

Next, define the `BelongsToManyWithManyKeys` relationship with the `belongsToManyWithManyKeys` method:
Next, define the `BelongsToManyKeys` relationship with the `belongsToManyKeys` method:

```php

public function auditors() {
return $this->belongsToManyWithManyKeys(
return $this->belongsToManyKeys(
User::class,
'id',
[
Expand Down Expand Up @@ -91,7 +91,7 @@ class User extends Model{
use HasExtendedRelationships;

public function audited(){
return $this->hasManyWithManyKeys(
return $this->hasManyKeys(
Post::class,
[
'created_by' => 'created',
Expand Down Expand Up @@ -126,7 +126,7 @@ This allows you to define multiple relationships between models with a single me

### Bonus Relationship

If you have a column posts in your users table which stores an array of local keys like [25, 60], you can use the following relationship:
If you have a column posts in your users table which stores an array of local keys like [7, 71], you can use the following relationship:

```php

Expand All @@ -140,9 +140,9 @@ class User extends Model
'posts' => 'array'
];

public function posts()
public function myPosts()
{
return $this->hasManyWithColumnKeyArray(
return $this->hasManyArrayColumn(
Post::class,
'id',
'posts'
Expand All @@ -156,17 +156,17 @@ When fetching data, you can retrieve the related posts with:

```php

$user = User::with('posts')->first();
$user = User::with('myPosts')->first();

// get posts with ids 25 and 60
$user->posts;
// get posts with ids 7 and 71
$user->myPosts;

```
This allows you to easily retrieve related records with an array of local keys, which can be useful in certain scenarios.

## Note:

Right now, the `belongsToManyWithManyKeys` and `hasManyWithManyKeys` methods work well with eager loading of the relation. However, when loading relations of a single model, the data may not be sorted as expected (e.g., in the order of "updater", "creator", etc.). Instead, all data will be returned as auditors. This functionality will be added in future updates.
Right now, the `BelongsToManyKeys` and `HasManyKeys` methods work well with eager loading of the relation. However, when loading relations of a single model, the data may not be sorted as expected (e.g., in the order of "updater", "creator", etc.). Instead, all data will be returned as auditors. This functionality will be added in future updates.

## Testing

Expand Down
24 changes: 12 additions & 12 deletions src/HasExtendedRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@

namespace Mrpunyapal\LaravelExtendedRelationships;

use Mrpunyapal\LaravelExtendedRelationships\Relations\BelongsToManyWithManyKeys;
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyWithManyKeys;
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyWithColumnKeyArray;
use Mrpunyapal\LaravelExtendedRelationships\Relations\BelongsToManyKeys;
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyKeys;
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyArrayColumn;

trait HasExtendedRelationships
{
/**
* @param string $related
* @param string|null $foreignKey
* @param string[]|null $relations
* @return BelongsToManyWithManyKeys
* @return BelongsToManyKeys
*/
public function belongsToManyWithManyKeys(string $related, string $foreignKey, array $relations): BelongsToManyWithManyKeys
public function belongsToManyKeys(string $related, string $foreignKey, array $relations): BelongsToManyKeys
{
$instance = new $related();
return new BelongsToManyWithManyKeys($instance->newQuery(), $this, $foreignKey, $relations);
return new BelongsToManyKeys($instance->newQuery(), $this, $foreignKey, $relations);
}

/**
* @param string $related
* @param string[]|null $relations
* @param string|null $localKey
* @return HasManyWithManyKeys
* @return HasManyKeys
*/
public function hasManyWithManyKeys(string $related, ?array $relations = null, ?string $localKey = null): HasManyWithManyKeys
public function hasManyKeys(string $related, ?array $relations = null, ?string $localKey = null): HasManyKeys
{
$instance = new $related();
return new HasManyWithManyKeys($instance->newQuery(), $this, $relations, $localKey);
return new HasManyKeys($instance->newQuery(), $this, $relations, $localKey);
}

/**
* @param string $related
* @param string|null $localKey
* @param string|null $foreignKey
* @return HasManyWithColumnKeyArray
* @return HasManyArrayColumn
*/
public function hasManyWithColumnKeyArray(string $related, ?string $foreignKey, ?string $localKey):HasManyWithColumnKeyArray
public function hasManyArrayColumn(string $related, ?string $foreignKey, ?string $localKey):HasManyArrayColumn
{
$instance = new $related();
return new HasManyWithColumnKeyArray($instance->newQuery(), $this, $foreignKey, $localKey);
return new HasManyArrayColumn($instance->newQuery(), $this, $foreignKey, $localKey);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

class BelongsToManyWithManyKeys extends Relation
class BelongsToManyKeys extends Relation
{
/**
* The local keys of the parent model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Collection;

class HasManyWithColumnKeyArray extends HasMany
class HasManyArrayColumn extends HasMany
{
/**
* Set the base constraints on the relation query.
Expand Down
Loading

0 comments on commit ebbddd8

Please sign in to comment.