Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Laravel 4 - Eloquent replace null values.

Posted on 2014-02-04
3
Medium Priority
?
2,638 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
[X]
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
  • 2
3 Comments
 
LVL 111

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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 i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

670 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