Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 的开源库。免去做 非空 判断 的繁琐步骤。
- Host: GitHub
- URL: https://github.com/xiyusullos/nullable
- Owner: xiyusullos
- License: mit
- Created: 2017-03-22T08:28:53.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-24T03:00:20.000Z (almost 8 years ago)
- Last Synced: 2024-12-19T14:11:26.324Z (24 days ago)
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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