• Stars
    star
    104
  • Rank 328,634 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Handles saving empty fields as null for Eloquent models

Nullable database fields for the Laravel PHP Framework

Build Status Latest Stable Version Total Downloads License Buy us a tree

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

public function up()
{
    Schema::create('profile_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->nullable()->default(null);
        $table->foreign('user_id')->references('users')->on('id'); 
        $table->string('twitter_profile')->nullable()->default(null);
        $table->string('facebook_profile')->nullable()->default(null);
        $table->string('linkedin_profile')->nullable()->default(null);
        $table->text('array_casted')->nullable()->default(null);
        $table->text('array_not_casted')->nullable()->default(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 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:

$ composer require dyrynda/laravel-nullable-fields

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 Illuminate\Database\Eloquent\Model;
use Dyrynda\Database\Support\NullableFields;

class UserProfile extends Model
{
	use NullableFields;
	
	protected $nullable = [
		'facebook_profile',
		'twitter_profile',
		'linkedin_profile',
		'array_casted',
		'array_not_casted',
	];
	
	protected $casts = [ 'array_casted' => 'array', ];
	
}

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->facebook_profile = ' '; // Empty, saved as null
$profile->twitter_profile  = 'michaeldyrynda';
$profile->linkedin_profile = '';  // Empty, saved as null
$profile->array_casted = []; // Empty, saved as null
$profile->array_not_casted = []; // Empty, saved as null
$profile->save();

More information

Working with nullable fields in Eloquent models - first iteration

Working with nullable fields in Eloquent models - Part Deux - second iteration, covers the details of this package

Support

If you are having general issues with this package, feel free to contact me on Twitter.

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!

Treeware

You're free to use this package, but if it makes it to your production environment you are required to buy the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you support this package and contribute to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees here

Read more about Treeware at treeware.earth

More Repositories

1

laravel-cascade-soft-deletes

Cascading deletes for Eloquent models that implement soft deletes
PHP
935
star
2

laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models
PHP
449
star
3

laravel-efficient-uuid

PHP
305
star
4

phpstorm-laravel-code-style

PhpStorm code style to meet Laravel's contribution guidelines
293
star
5

laravel-defibrillator

Ensure your Laravel applications keep a normal pulse
PHP
167
star
6

laravel-make-user

A simple Artisan command to create application users
PHP
109
star
7

dotfiles

Lua
57
star
8

nomad

Laravel-style database migrations wherever they may roam
PHP
56
star
9

vagabond

Eloquent without a home
PHP
48
star
10

founder

Laravel starter application
PHP
48
star
11

carbon.vim

A Vim port of the IntelliJ Carbon theme
Vim Script
41
star
12

Laravel.tmTheme

A Sublime Text 3 theme based on the Laravel documentation colour scheme
40
star
13

founder-style

Founder repository style guide
17
star
14

laravel-owns-models

PHP
15
star
15

readme-generator

Generate README files that rock!
HTML
15
star
16

laravel-google-cse

Laravel wrapper for the Google Custom Search Engine API
PHP
9
star
17

laravel-ldap

Leverage Laravel's middleware to ensure your LDAP-authenticated users stay that way
PHP
6
star
18

git-workflow

A git workflow for facilitating client-facing User Acceptance Testing
6
star
19

RestClient

A REST API Client
PHP
4
star
20

laracasts-saving-json-into-database

Tests and implementation for forum post
JavaScript
4
star
21

terminable-jobs

PHP
3
star
22

telstra-sms

PHP
2
star
23

dyryme

dyry.me link shortener
PHP
2
star
24

amber-electric-php

PHP
2
star
25

simple-menu

PHP
1
star
26

zsh-completions

zsh completion scripts
Shell
1
star
27

chalrynda-blog

Michael & Rhiannon's travel blog
CSS
1
star
28

foods-for-life

Foods For Life Bedrock-based WordPress site
PHP
1
star
29

devdojo-events

PHP
1
star
30

deckset

1
star
31

bbc-good-food

PHP
1
star
32

hemp

PHP
1
star
33

s3upload

Code for blog series on uploading files to Amazon S3 from the browser
PHP
1
star