Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Laravel 4 - Eloquent replace null values.

Posted on 2014-02-04
3
2,461 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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to count occurrences of each item in an array.

840 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