Querying and Eager Loading complex relations in Laravel

Laravel is a PHP framework that uses Eloquent, a powerful and amazing ORM that allows you to do complex SQL queries in a very easy way. But sometimes you need more, and here I’m gonna give you an interesting tip that can bring you a lot of flexibility.

When dealing with simple database queries in Laravel maybe you don’t have so many challenges, because it’s really very easy to use. But when your app starts growing or even your boss needs some type of reports, then you start thinking “How can I do that with Laravel”?

In some cases, you know how to do that using pure SQL, but it’s not so good to write a beautiful code and suddenly a SQL query in the middle of everything. Then you start trying to find how you can do that with Eloquent.

Sometimes you have to query and use the matching results, the same ones, actually. This is a case where the same logic you use for filtering is used for the relation itself. I’m gonna explain it better below.

Continue reading Querying and Eager Loading complex relations in Laravel

Solitary or Sociable? Testing Events and Listeners using Laravel

Testing with Laravel is very easy, but it can be a nightmare when the tests depend on Events and Listeners. In this post I’m gonna show you how you can simplify and improve those tests.

Laravel is one of the most popular PHP frameworks nowadays, and I’d say its Event handler is one of the most powerful features. Using events in Laravel is usually very easy, but when your app gets bigger you can get in trouble with previously written tests.

When testing events in Laravel, you can fake() events in a very simple way, like the documentation says. The problem is not with the events, but with the listeners, because usually a listener does a single action, but with more than one listener you start writing repeatable tests.

Continue reading Solitary or Sociable? Testing Events and Listeners using Laravel

QueryFilter: A Model Filtering Concept

Having a clean code with single responsibility is important, and doing that for model filtering can be easy and very powerful. Believe me.

This blog post is related to a talk I gave on April, 2018 on Darkmira Tour PHP 2018, in Brasília/DF, Brazil, with the same title. The slides are on SpeakerDeck.

Filtering models was, for a very long time, a hard task for me. I admit that I could not think in some easy way to do that. I tried, refactored some code, created custom classes for that, but I never thought how this could be easily implemented.

Watching a Laracast’s video from 2016 about the Laravel’s Eloquent ORM I faced of with a bunch of classes and a trait that removed a lot of trash from my controller actions. That was called by Jeffrey Way the QueryFilter.

Continue reading QueryFilter: A Model Filtering Concept

Congratulations! Now you are a remote worker. Enjoy your new life!

Working remotely is the dream job of most developers I know. It’s a hard process but it’s totally possible. Now you got the best job you could . Enjoy your new life!

The beginning. The dream.

Do you remember yourself some weeks/months ago? The first interview you did (maybe you’re not a native English speaker like me). You’ve improved a lot, you’re a better person. You’re speaking a “better English and you’re not shy “in front of” the interviewers anymore.

After resumes and more resumes, after those initial interviews, those technical ones, after doing a “sample project” to show up your skills, you got your dream job. You received an offer and you accepted it. Now you’re officially a remote worker! Congratulations to you and to your wife, because now you’re going to be at home every day, and this is really important!

Now you can work from anywhere, even traveling. Now you can be a digital nomad. Now you can work without pants, with slippers and you can wake up just a few minutes before start working. No more commuting, no more offices.

Next week you’re going to start a new step in your life. Be prepared, because your life’s going to start changing forever. I said forever.

Continue reading Congratulations! Now you are a remote worker. Enjoy your new life!

Open-source is about sharing and giving back. Think about that.

Maintaining an open source project – even a small one – is not an easy task. The open source ecosystem is about sharing and contributing, about giving and receiving. You scratch my back and I will scratch yours.

Open-source is not only a free and open software, it’s a lifestyle. Working with open-source is working for free, making the other’s life better. It’s like someone that works helping people through an NGO (Non-Governmental Organization). They do the better expecting nothing back.

According to the Oxford Dictionary open-source is “Denoting software for which the original source code is made freely available and may be redistributed and modified”. Pay attention to these two words: redistributed and modified. This means that you can change the original code, can update it to your needs, and can share it back to the community. Remember this.

Continue reading Open-source is about sharing and giving back. Think about that.

Degree or not degree? When is it really important?

One of the most discussed topics in the technology field is about having degrees and how important they are. Do you really need one to be a good engineer? When is it really important for you?

Writing a post like this is a big responsibility. I’m not here to say if you should attend to a university degree, even the opposite. I’m here to share my experience and make you think about this subject with at least two different points of view. Since 2016 I’m on the employee side of the desk, but I stayed on the employer one for nine years, including hiring.

Continue reading Degree or not degree? When is it really important?

Easy server provision for PHP environments using ServerPilot

Learn how to use a very simple and efficient service called ServerPilot for provisioning a web server to host your next PHP project or blog.

Provisioning a well-configured web server for a PHP application is not an easy task. You can have experience doing that, but this is not my strongest skill, I admit. So I’m always looking for new services and tools to make this job easier for me, and faster.

I’m a big fan of Laravel Forge and the company I run for years was (and is) a very happy Forge customer. Unfortunately, my goal, for now, was to find a place to host my blog, after some changes I’ve made in the last weeks.

I run my blog using Jekyll and GitHub Pages for years, but I was writing less then I expect. I like the WordPress platform, and mainly writing on its editor, so I decided to move back to WP. Then I create a new droplet on DigitalOcean using their LAMP box. The droplet was running well, but sometimes Apache could not handle everything well, then I needed to add an NGINX reverse-proxy to make things easier. There’s when I discovered ServerPilot.

Continue reading Easy server provision for PHP environments using ServerPilot

Invest in yourself. Be searchable!

Being really good in your field is important, but more important than that is to seem to be good. Invest in yourself. Be searchable!

How many times did you search your name on Google? How many times did you search some keywords related to you? Did you like the results you got? Can them be better? They should.

We, developers and engineers, built the Web like it is today, but sometimes we forgot how important we are, and mainly, how important we should be. This is something that – believe me – happens more than you think. We like to code for others, to create software, to make money, but we forget to invest in ourselves images, and how people think we are.

This is not about your work, this is not about your job. This is about you, your image, and how you take care of it.

Continue reading Invest in yourself. Be searchable!

Why you should contribute to open source projects

Open source can change your life. It has changed mine with Corcel, an open source project I started in 2013 that changed who I am, and how I live.

The first title I gave to this post was How open source can change your life for the better. That was a good title too, but I think the main reason I wrote this post is to encourage you to start (or continue) working and contributing to open source projects, that’s why the title changed.

You can think: “open source cannot give me any money”. The fact is: directly no, maybe a few. Contributing to (or creating) open source projects show your code to the world. And that’s the point! It’s when your life starts changing, believe me.

My life started changing in 2013, and because of open source. In that year I was running my company, an advertising agency. I was the one responsible for all the web and development fields. Actually, the company started in 2007 and I was in the heart of the development team for many years.

Continue reading Why you should contribute to open source projects

Using XDebug with PHPUnit in PHPStorm

Learn how to configure XDebug and PHPUnit in PHPStorm, allowing you to write better tests and fix bugs faster.

Test Driven Development (TDD) is an old topic, I know, but it seems many people don’t understand how it makes you write better code. The point here is that one of the most important benefits it’s to use debugging tools together with tests, making your flow more efficient.

I started using TDD on the command line, and still use it sometimes, but since I started using PHPStorm and decided to try how it handles tests, and that’s amazing! When you add debugging tools – like XDebug – to it everything starts making sense, then you have the feeling you’re on the right path.

PHPStorm has a dedicated interface to run and debug tests, almost in the same window, what makes the process of writing code safer and easier.

Continue reading Using XDebug with PHPUnit in PHPStorm