Jun 18, 2014
So, there's a problem I've been running into a lot while I continue my work on this new portfolio. I should probably mention that this current incarnation of the website is the third. The previous one is live while I write this, and full of terrible design choices because it's the first website I ever created. The one after that was actually started as a Python project, but I ran into problems when I realized I didn't actually know Python.
Of course, now that I've actually got somewhat of a handle on the Laravel version of this project, it now suffers from what developers call feature creep. This is why it's taken at least a year to get even to this - quite limited - point. Laziness is also a factor (working from home is not my strong point).
Then, yesterday I ran into a rather sudden realization. After changing the design of this website at least three times, not including the previous attempts I just mentioned, I still hate the design.
This is almost funny, because I thought I did like it for a while. Unfortunately, while I've gotten in some great practice with designing business websites, artistic websites are much more difficult. They are a whole other monster, because you get no guidelines to start with. Looking for a website design for a travel business? Yeah, sure, I can spit one out in a couple hours. Artistic websites? Heh, good luck.
Freedom is a curse, especially when it comes to web design. One the one hand, I have to try to show off what I can do. This is complicated by the fact that I'm a web designer and artist. Somehow I have to mesh those two together, and that's a lot harder than it sounds; I'm used to designing boring websites for traditional businesses. Designing for an artist requires thinking outside of the box, and for web design, that's something you get trained to stop doing after having such designs rejected. "Don't reinvent the wheel" is the first thing you learn as any type of designer, but you have to step outside your comfort zone just a few steps to make something awesome.
Then again, step too far and you'll end up with the pile of poo I had the first time around. That was just sad.
This has lead to the sad mixture that is my current website.
Rather than redoing all of it, I'm trying my hardest to actually finish the thing so that I can at least present it to people. Anything is better than my live website.
Mar 1, 2014
Previously, I gave a solution for this problem which involved using the use keyword in a PHP closure.
I failed to realize that none of this logic should be in the controller anyway until later. Here is a quick update of my code, which I moved to the Eloquent model. I created two methods; one to pull the previous record, and one to pull the next record.
public function get_next_by_date(){
return Artwork::select('id', 'title', 'created_at')
->enabled()
->where('created_at', '>', $this->created_at) //where the art is newer
->orWhere(function($query) //or, if the art was made on the same day and the id is greater
{
$query->where('created_at', '=', $this->created_at)
->where('id', '>', $this->id);
})
->orderBy('created_at')->orderBy('id') //order by oldest first and smallest id first
->first(); //get just one record
}
By using a model's method to grab the next/previous records, it's more clear to the reader and eliminates the need for the
use keyword.
Feb 27, 2014
During
my journey to create the greatest portfolio of all time, I came
across many design flaws in my own software. These things tend to
happen a lot whenever you're trying something new, or exploring
domains that you're not completely familiar with.
I've
worked with Laravel before, but I'm still considered a newbie or
maybe a novice when it comes to web programming. I've always been
more focused on visual aspects and user experience. In other words,
I'm not a PHP expert. I know how to make animated boxes and columned
layouts with CSS3 and HTML5. I pay attention to Bootstrap's Github.
But whenever I try to read blog posts by Laravel creators, I tend to
end up confused and read it twice for good measure.
Most
of these problems are things I encounter from lack of experience with
them. Just like in order to solve IE compatibility problems requires
direct experience, solving these issues also requires such
experience. This is why StackOverflow is a good thing.
http://stackoverflow.com/questions/5203257/mysql-next-previous-record
The
problem was almost verbatim the above question.
I
wanted to pull the next and previous records by their dates. The
problem was of course that some records had the same dates. This
meant that my attempted next and previous buttons encountered
infinite loops whenever the next record was the same date as the
current.
The
solution above works out perfectly. All I did was add a couple
parenthesis to the OR WHERE statement, and transcribed all of it to
Laravel 4.
$next_record = Artwork::select('id')
->where('created_at', '>', $artwork->created_at) //where the art is newer
->orWhere(function($query) use ($artwork) //or, if the art was made on the same day and the id is greater
{
$query->where('created_at', '=', $artwork->created_at)
->where('id', '>', $artwork->id);
})
->orderBy('created_at')->orderBy('id') //order by oldest first and smallest id first
->first(); //get just one record
$prev_record = Artwork::select('id')
->where('created_at', '<', $artwork->created_at) //where the art is older
->orWhere(function($query) use ($artwork) //or, if the art was made on the same day and the id is lesser
{
$query->where('created_at', '=', $artwork->created_at)
->where('id', '<', $artwork->id);
})
->orderBy('created_at', 'desc')->orderBy('id', 'desc') //order by newest first and largest id first
->first(); //get just one record
Note
the funny bit in the orWhere method. Those functions are known as
anonymous functions, closures, or lambda functions. They're the sexy
new kid on the block in PHP.
In
order to use my $artwork object I had created earlier in my closures,
which is an Eloquent model, I had to add the "use" keyword.
If you have never seen the use keyword, read about it here: http://blog.dubbelboer.com/2012/04/07/php-use-keyword.html
As
Erik Dubbelboer writes, there are three ways to pass variables to
your closures. In my case, the use keyword did just the trick I
needed, allowing me to use my local variable in the scope of the
closure.