Skip to content

Commit

Permalink
Improve docs (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
zingimmick authored Nov 28, 2021
1 parent b557ead commit 7166eb0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Laravel Eloquent Relationships

<p align="center">
<a href="https://packagist.org/packages/zing/laravel-eloquent-relationships"><img src="https://poser.pugx.org/zing/laravel-eloquent-relationships/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/zing/laravel-eloquent-relationships"><img src="https://poser.pugx.org/zing/laravel-eloquent-relationships/downloads" alt="Total Downloads"></a>
Expand All @@ -14,6 +15,98 @@ Require Laravel Eloquent Relationships using [Composer](https://getcomposer.org)
composer require zing/laravel-eloquent-relationships
```

## Usage

### BelongsToOne

`BelongsToOne` is based on `BelongsToMany`

#### Difference:

- returns related model instead of collection of models
- returns `null` instead of empty collection of models if the relationship does not exist
- supports return default related model in case the relationship does not exist

#### Example:

```php
<?php

use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\BelongsToOne;
use Zing\LaravelEloquentRelationships\Tests\Models\User;

class Group extends Model
{
use HasMoreRelationships;

public function leader(): BelongsToOne
{
return $this->belongsToOne(User::class)
->wherePivot('is_operator', true);
->withDefault(function (User $user, self $group): void {
$user->name = 'leader for ' . $group->name;
});
}
}
```

### MorphToOne


`MorphToOne` is based on `MorphToMany`

#### Difference:

- returns related model instead of collection of models
- returns `null` instead of empty collection of models if the relationship does not exist
- supports return default related model in case the relationship does not exist

#### Example:

```php
<?php

use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\MorphToOne;
use Zing\LaravelEloquentRelationships\Tests\Models\Product;

class Image extends Model
{
use HasMoreRelationships;

public function bestProduct(): MorphToOne
{
return $this->morphedByOne(Product::class, 'imageable', 'model_has_images');
}
}
```

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\MorphToOne;
use Zing\LaravelEloquentRelationships\Tests\Models\Image;

class Product extends Model
{
use HasMoreRelationships;

public function cover(): MorphToOne
{
return $this->morphToOne(Image::class, 'imageable', 'model_has_images')->withDefault([
'url' => 'https://example.com/default.png',
]);
}
}
```

## License

Laravel Eloquent Relationships is an open-sourced software licensed under the [MIT license](LICENSE).
5 changes: 3 additions & 2 deletions tests/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\MorphToOne;

/**
* @property string $url
Expand All @@ -21,12 +22,12 @@ class Image extends Model
*/
protected $fillable = ['url'];

public function bestProduct(): \Zing\LaravelEloquentRelationships\Relations\MorphToOne
public function bestProduct(): MorphToOne
{
return $this->morphedByOne(Product::class, 'imageable', 'model_has_images');
}

public function defaultProduct(): \Zing\LaravelEloquentRelationships\Relations\MorphToOne
public function defaultProduct(): MorphToOne
{
return $this->morphedByOne(Product::class, 'imageable', 'model_has_images')->withDefault([
'name' => 'default name',
Expand Down
8 changes: 5 additions & 3 deletions tests/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Zing\LaravelEloquentRelationships\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\BelongsToOne;
use Zing\LaravelEloquentRelationships\Relations\MorphToOne;

/**
* @property string $name
Expand All @@ -30,19 +32,19 @@ public function leader(): BelongsToOne
->withPivot('status');
}

public function images(): \Illuminate\Database\Eloquent\Relations\MorphToMany
public function images(): MorphToMany
{
return $this->morphToMany(Image::class, 'imageable', 'model_has_images');
}

public function cover(): \Zing\LaravelEloquentRelationships\Relations\MorphToOne
public function cover(): MorphToOne
{
return $this->morphToOne(Image::class, 'imageable', 'model_has_images')->withDefault([
'url' => 'https://example.com/default.png',
]);
}

public function thumbnail(): \Zing\LaravelEloquentRelationships\Relations\MorphToOne
public function thumbnail(): MorphToOne
{
return $this->morphToOne(Image::class, 'imageable', 'model_has_images');
}
Expand Down

0 comments on commit 7166eb0

Please sign in to comment.