Solved

Laravel 4 - Eloquent replace null values.

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
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.

680 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