定制 bluecode/laravel-nullable-field 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bluecode/laravel-nullable-field

Composer 安装命令:

composer require bluecode/laravel-nullable-field

包简介

This trait allows you to easily flag attributes that should be set as null.

README 文档

README

Often times, database fields that are not assigned values are defaulted to null. This is particularly important when creating records with foreign key constraints.

Note, the database field must be configured to allow null.

More recent versions of MySQL will convert the value to an empty string if the field is not configured to allow null. Be aware that older versions may actually return an error.

Laravel (5.1) does not currently support automatically setting nullable database fields as null when the value assigned to a given attribute is empty.

Installation

This trait is installed via Composer. To install, simply add it to your composer.json file:

{
	"require": {
		"bluecode/laravel-nullable-field": "~0.1"
	}
}

Then run composer to update your dependencies:

$ composer update

In order to use this trait, import it in your Eloquent model, then set the protected $nullable property as an array of fields you would like to be saved as null when empty.

<?php

use Bluecode\Traits\NullableField;
use Illuminate\Database\Eloquent\Model;

class UserProfile extends Model
{
	use NullableField;
	
	protected $nullable = [
		'tel',
		'address'
	];
}

Now, any time you are saving a UserProfile profile instance, any empty attributes that are set in the $nullable property will be saved as null.

<?php

$profile = new UserProfile::find(1);
$profile->tel = ' '; // Empty, saved as null
$profile->address  = '10 A Street';
$profile->save();

Support

If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.

If you're using this package, I'd love to hear your thoughts. Thanks!

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-05