Solving a Mysterious Case of Empty Eloquent Model in Laravel

I’m working on a Laravel application that basically lets users make collections of eBay products. For those collection I use a model called ProductList. I initially wanted to call that model simply List, but that is a php reserved word.

Because I wasn’t happy with how wordy Product List sounds, I decided to name my route simply lists. I can view all lists going to GET /lists, or POST to that same url to make a new list, but when I tried to got to GET /lists/1 to view the first list, I would not get what I expected. Instead of a populated ProductList model in my controller, I was getting an empty model. The first symptom I saw was that trying to output the list name in my blade template resulted in nothing being printed out.

This got me scratching my head for a little bit. The controller’s method was well declared, so I referred to the documentation to see what might be going wrong. None of my previous experiences working with Laravel had had this kind of issue, but looking at the docs is always a good start.

The documentation states “Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name”. The important part here is “whose type-hinted variable name match a route segment”. The show method of the controller created using artisan had a variable type-hinted as ProductList named $productList. Now I just had to check if the route segment matched that name. For doing that, since my routes file uses the resource method to declare the lists route which means I can’t see the route in the routes file, all I had to do was php artisan route:list on the command line, and the list of all routes was printed out. Sure enough the route segment, and the type-hinted variable didn’t have a matching name. All I had to do now was edit the show method’s signature so that the type-hinted variable had a matching name with the route segment. After doing that, everything worked just fine.

Advertisements

The Case of the Drupal Form that Would Not Post

This is the story of a Drupal form that would not handle data submissions correctly when a large number of fields were present.

Working on a Drupal site, I came across the strange case of a settings form that would not save its values when a large number of fields were present. The form helps site admins associate certain vehicle model numbers to their appropriate model name, class, and group. A class is basically a collection of similar models, and a group is a collection of similar body types such as sedan, coupe, or SUV. We have many model numbers, most of which have no actual inventory on existence, so we don’t really display those in the settings form, but yesterday we found a bug that requires us to display all model numbers. When all model numbers are being displayed in the form, the form stops working properly, and no submission is ever saved.

The form submission process in Drupal is rather complex, but all you need to know at this point is that Drupal sets an element process_input in the $form_state array that it passes around the functions that handle form processing. If process_input is set to true, then the form submission is handled, otherwise it is not. This happens in the form_builder function defined in includes/form.inc. In order for process_input to be set to truethe form must either be a programmed form, or the $form_state['input']['form_id'] variable must be defined and equal to the $form_id variable that Drupal passes around the form processing functions.

In my case the form was failing to process because the $form_state['input']['form_id'] variable was set to null. At this point, you need to know that $form_state['input'] is set to the value of $_POST for forms submitted using the POST method. Looking at both, the form, and the parameters it sent in the request, I could see that there was a form_id input, and that it was being sent with a value equal to $form_id but PHP had no knowledge of it. It turns out the request wasn’t being fully caught by PHP.

At this point, my guess is that the request is too big for my server settings, or there are too many fields being sent. PHP has settings for both, the maximum size of a POST request that it handles, and the maximum number of input variables. As far as I know there are two places were you can modify these settings: the php.ini file, and the .htaccess file. The preferred way would be to modify the php.ini file, but depending on your host you may not have access to that. In that case, using an .htaccess file would be your best option.

In this stackoverflow question you can get an idea of how to go about modifying the values in case you ever encounter the same issue.

CSS Margin VS Padding

Today, while working on a tiny project, I suddenly wondered why I usually favor margins instead of paddings to create space around elements in CSS. At the moment, given that there are deadlines, I decided to push aside the question, but before that I did a quick search, and opened a Stack Overflow question about the matter so I would not forget about this existential question of mine.

At some point during that tiny project, I came up with a situation that definitely required paddings, and not margins to solve. The situation was this: I had two elements in a sort of lightbox effect. I wanted the “lightbox” to close whenever the user clicked on the element that was sitting behind covering the full window, but not when the user interacted with the element sitting in front, ie the content of the lightbox. The solution was simple, add a click event handler on the back element that closed the lightbox, and a click event handler in the content element that only stopped the propagation of the event. That way whenever the user interacted with the content element the click event would never reach the back element, thus not closing the lightbox.

This method worked well, but I wanted users to have a little room around the content where they could safely click without closing the lightbox. For this, using a padding on the content element was the way to go. In this situation a margin would not have worked because you can ‘t click the margin of an element. Clicking the element was required because only that way could we prevent the click event from bubbling up to the back element.

In the stack overflow question, someone mentions that they usually use padding only when they want to increment the space inside a visible box. I’ve now presented you with another case, although this ones is more of an edge case.

I think most of us prefer margins, but there are cases where padding is the right and maybe only way to go.

orWhereHas in Laravel 5.3

I was recently tasked with updating a project from laravel 5.2 to 5.3. I know even 5.3 is an old version now, but the project has fallen behind on laravel versions for some reason. The update was rather simple, and most of what I had to do was make sure that request validations were properly defined given how laravel 5.3 changed the way validations are handled. However, a couple of days ago I received a notification that something wasn’t right on the platform, and it was returning the wrong results for certain filtered assets.

Since the recent work that had been done in the files that were involved in the broken feature had nothing to do with the problem, the first suspect was the laravel update. After much digging I found out that a rather complex database query had an ‘AND’ in laravel 5.2, but an ‘OR’, when using laravel 5.3. For some reason that I still ignore a model scope was being initialized with orWhereHas. Laravel 5.3 changed how Eloquent handles those cases, and it “now respect[s] the leading boolean of scope constraints” according to the upgrade guide.

In laravel 5.2 the leading boolean wasn’t respected, and scope constrainst were always constructed using AND. This was problematic in most cases where people wanted to start their scopes with OR. However, in this case, AND was the correct way to construct the query. The fact that the program worked before the update was mere coincidence. An unintentional use of a ‘feature’ in laravel 5.2 that was changed (should I say a bug that was fixed?) in laravel 5.3.

Given the fact that I ignore why the original developer felt it was necessary to use orWhereHas in the (rather complex) scope, I decided to pass the issue along the chain to the original developer in hopes he would have a better ideas of whether changing the orWhereHas for a simple whereHas would have other implications.

You can read the upgrade guide, or this stack overflow question about leading booleans in eloquen scope constraints, or the original pull request for the change in laravel’s eloquent to learn more about the change that laravel 5.3 introduced in regards to how it constructs queries when eloquen scopes are used.

Ruby Bundler Install Error on Mac OS

I migrated a project a couple of days ago from Fedora to Mac OS. The project uses bundle to install the required ruby gems, and I ran bundle install when I migrated the project, so today I expected it to just work. But it did not.

One of the gems the project uses is sass, and it also uses a watch task in grunt to compile sass files automatically, However, when I edited a file, I got an error. Sass was not installed. I ran bundle install again, and noticed that the command was returning some errors about certain files not being writable. This is because of the ‘rootless‘ feature in Mac OS, which prevents anybody, even root, from writing to certain locations. One of those locations is /usr/bin, which bundle was trying to write to to add the gem executable files. No wonder I was getting a not-istalled error for sass. However, I hadn’t really realize that just jet.

I decided to investigate the issue, and found that some people who had the same problem fixed it by adding certain location to their path. This is when I actually understood what was going on, because even though I had seen the bundle errors, I hand’t really understood the issue. As I said before, bundle was trying to write to a location where it could not. I.e. /usr/bin.

I did some more researching and found some information about the gemrc file, and how it can be used to tell gem where it should install gems. I created a gemrc file, and tried bundle install. The error didn’t get fixed.

I decided to look for the documentation for bundle install, and there I found that you can specify where gems should be placed, and where the executable files should be placed. I finally ran the command

bundle install --force --path=~/ --binstubs=~/bin

It failed with error

Errno::EACCES: Permission denied @ rb_sysopen – /Users/me/bin/bundle
An error occurred while installing bundler (2.0.0), and Bundler
cannot continue.

AT this point I was loosing hope, and getting frustrated, but I continued looking for an answer. That is how I found that I probably had the wrong permissions for bundle. I listed the contents of my ~/bin directory, and found that the bundle file was owned by root. I changed ownership of the file to me, and this finally got everything working correctly.

It may seem like I fixed this issue in no time, but the reality is that I spent over two hours trying until finally being able to make it work. I should say, though, that I am no ruby developer. This project is the only place were I’ve ever used bundle, and gems that I can think of. Plus, I’m finding out that the ‘rootless‘ feature is at a minimum as annoying as selinux, but I would never, ever, advice anybody to disable it, nor would I do it in my own computer:

Setting Up Canon G3111 on Mac OS Mojave

Today I was setting up the home printer on the Mac OS Mojave machine, and, although there is a driver download page from canon, there is no download button, or at least I  couldn’t find one. The printer doesn’t seem to support AirPrint, and none of the other options in the printer settings seemed to work. The solution: find a driver.

A quick search got me to this page https://ijcanon.com/canon-pixma-g3110-drivers-download/ where I selected the CUPS driver for Mac OS, and went through the set up process. After that was done, I went to system preferences > Printers & Scanners, and clicked the plus button. The printer was listed in the Default section, and I selected it as I had been doing previously, and again, no driver was auto-selected for it. I selected the “Select software” option in the section labeled “Use”, and this time, there was a G300 driver option on the list that appeared. That option wasn’t there previously. I selected that, and Clicked Add to add the printer. The printer is now set up and working.

Drupal Error: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1231 Variable ‘sql_mode’ can’t be set to the value of ‘NO_AUTO_CREATE_USER’ in lock_may_be_available()

I was setting up a drupal 7 site on a Mac OS computer, and got the error in the title when trying to sync the local DB with a remote DB. After a lot of time, the problem came down to a mysql incompatibility. I had installed MySQL 8, and had to downgrade to MySQL 5.7.24. The full error message contained a reference to the file includes/loc.inc line 167 as the source of the exception, but scrolling a little up the file I saw a catch statement in the lock_acquire function, and decided to dump the error caught. It turns out the exception is actually thrown on file includes/database/mysql/database.inc, line 94, in the constructor of the DatabaseConnection_mysql object. In the constructor is where Drupal tries to set sql_mode to a value which MySQL 8 is rejecting. For more information visit https://dev.mysql.com/doc/refman/8.0/en/mysql-nutshell.html#mysql-nutshell-removals

The Weird Case of the Modem that Would Work With Only One of the Devices Connected to It.

I received a message this morning from my brother, who said to be having problems with his modem. He said, that all the sudden all the devices connected to it stopped getting internet service, except for one. The devices seemed to be connected, but the internet would not work on any of them, except for the Roku they use to make their TV a little smarter. I said I would stop by to check it later, and so I did.

Just like he had explained, only the Roku could connect to the internet. My initial thought was that the Roku, for some reason was hugging the internet all for its own, so I disconnected it, and nothing changed. The other devices still were unable to connect to the internet. I knew for sure that the devices were connected and that the modem was recognizing them because I could see them in the modem’s settings panel.

I must have spent over half an hour looking at all the settings in the modem over and over, changing channels, and other settings with no luck. When I was about to give up, it occurred to me that it could be a DNS issue. This right after my brother demonstrated how WhatsApp was the only internet app that seemed to work. I updated the DNS in a Mac we had wired to the modem, and just as I hit the Apply button, a bunch of notifications and what not started showing up in the screen. Vioala!

I adjusted the DNS settings on the modem, and then all the connected devices started working well. I don’t know who his ISP is, and why in the world their DNS would stop working, but I’m glad there are open DNS servers. As far as for why the Roku was the only device that was working well, it seems Roku uses custom DNS.

Debugging Apache Virtual Host Config

This is just a quicktip I found while doing a quick search for the sintax for a virtual host in apache: You can run apachectl -t -D DUMP_VHOSTS to run config test for virtual hosts. This will report any error there may be with your config. I found this is a great way to debug virual host config files. You can read more about apachectl on https://httpd.apache.org/docs/current/programs/apachectl.html