Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xiyusullos/nullable

chain calls on object whatever the object is null or not. 一个可以在 Laravel 上,链式 调用 Model 的 relationship 的开源库。免去做 非空 判断 的繁琐步骤。
https://github.com/xiyusullos/nullable

Last synced: 22 days ago
JSON representation

chain calls on object whatever the object is null or not. 一个可以在 Laravel 上,链式 调用 Model 的 relationship 的开源库。免去做 非空 判断 的繁琐步骤。

Awesome Lists containing this project

README

        

# Nullable - Chain calls on Null for PHP

## Installation

Install the latest version with

```bash
composer require xiyusullos/nullable
```

## Usage

### Basic Usage

```php
a->b->c;
```

### Laravel Usage

```php
belongsTo(User::class);
}

// ...
}

class User extends Model
{
use Nullable;

public function profile()
{
return $this->hasOne(Profile::class);
}

// ...
}

class Blog extends Model
{
use Nullable;

public function user()
{
return $this->belongsTo(User::class);
}

// ...
}

// wanna get the writer's department name who posted the blog #1

// without Nullable
$blog = Blog::find(1);
$user = $blog->user;
if ($user) {
$profile = $user->profile;
if ($profile) {
$departmentName = (string) $profile->departmentName;
}
} // that's so annoying!

// with Nullable
$blog = Blog::find(1);
$departmentName = (string) $blog->user->profile->departmentName;
```

## About

### Author

[xiyusullos](http://xy-jit.cc)

### License

`Nullable` is licensed under the MIT License - see the `LICENSE` file for details