All Projects → michaeldyrynda → Laravel Nullable Fields

michaeldyrynda / Laravel Nullable Fields

Licence: mit
Handles saving empty fields as null for Eloquent models

Projects that are alternatives of or similar to Laravel Nullable Fields

Laravel Cascade Soft Deletes
Cascading deletes for Eloquent models that implement soft deletes
Stars: ✭ 498 (+465.91%)
Mutual labels:  eloquent, eloquent-models, laravel, package
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (+170.45%)
Mutual labels:  eloquent, eloquent-models, laravel
Elasticquent
Maps Laravel Eloquent models to Elasticsearch types
Stars: ✭ 1,172 (+1231.82%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Lucene Search
Laravel 4.2, 5.* package for full-text search over Eloquent models based on ZF2 Lucene.
Stars: ✭ 75 (-14.77%)
Mutual labels:  eloquent, eloquent-models, laravel
Watchable
Enable users to watch various models in your application.
Stars: ✭ 65 (-26.14%)
Mutual labels:  eloquent, laravel, package
Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
Stars: ✭ 196 (+122.73%)
Mutual labels:  eloquent, eloquent-models, laravel
Eloquentfilter
An Eloquent Way To Filter Laravel Models And Their Relationships
Stars: ✭ 1,113 (+1164.77%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (+7.95%)
Mutual labels:  eloquent, laravel, package
Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (+550%)
Mutual labels:  eloquent, laravel, package
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (+577.27%)
Mutual labels:  eloquent, laravel, package
Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (-10.23%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (+55.68%)
Mutual labels:  eloquent, eloquent-models, laravel
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (+37.5%)
Mutual labels:  eloquent, laravel, package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+125%)
Mutual labels:  eloquent, laravel, package
Searchable
Search/filter functionality for Laravel's Eloquent models
Stars: ✭ 113 (+28.41%)
Mutual labels:  eloquent, eloquent-models, laravel
Plans
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Stars: ✭ 326 (+270.45%)
Mutual labels:  eloquent, laravel, package
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-19.32%)
Mutual labels:  eloquent, laravel, package
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+609.09%)
Mutual labels:  eloquent, laravel, package
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (-11.36%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Mentions
End-to-end mentions in Laravel 5.
Stars: ✭ 68 (-22.73%)
Mutual labels:  laravel, package

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].