Solved

Laravel 4 - Eloquent replace null values.

Posted on 2014-02-04
3
2,382 Views
Last Modified: 2014-02-10
I have a laravel web application that is nearly completed. I have had to switch database servers and have run into an unexpected problem. The new database does not allow null values. Other applications read and write to this database which is why nulls are not allowed. Therefore I will not be able to change the database. What would be the easiest way to check for null values before a save?
0
Comment
Question by:HelpDeskGeiger
  • 2
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39832629
I think you might want to set up a test of this.  When tables are created by CREATE TABLE the column definitions can be set to NOT NULL and can be set to contain a DEFAULT value.  When your script INSERTs data into a row, any columns that are not included are given their default value.  If you INSERT an empty string into a NOT NULL column, the data base will keep it and will return an empty string to the SELECT queries.

So you may be OK right now, assuming that the database tables are defined correctly.
0
 

Accepted Solution

by:
HelpDeskGeiger earned 0 total points
ID: 39835685
The solution was to setup mutators in the model. even though the column name was in snake case I still had to use camel case for the function name. so SALES_ORDER_NUMBER became SalesOrderNumber. here is an example:

protected function getSalesOrderNumberAttribute($value) { return $this->attributes['SALES_ORDER_NUMBER']; }
protected function setSalesOrderNumberAttribute($value) { $this->attributes['SALES_ORDER_NUMBER'] = (is_null($value) ? '' : $value); }

Open in new window

0
 

Author Closing Comment

by:HelpDeskGeiger
ID: 39846809
found the answer in the laravel docs that did not force me to change the database.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now