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

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.

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

Using Ruby on Rails Ajax adapter with any project, even PHP (like Laravel)

Hello!!!

Every project you have to make some requests using Ajax, right? I know that jQuery has done a very good job and almost all project you start it is present there, together with your Javascript files. So you can make Ajax calls any time, where you want, just making the call using Javascript.

I developed some projects using Ruby On Rails some time ago, and I start to think about using AJAX in a different way. Rails uses a custom jQuery adapter to allow you to do things like that:

link_to("Destroy", user_path(@user), method: :delete, remote: true)

Continue reading Using Ruby on Rails Ajax adapter with any project, even PHP (like Laravel)

Why WordPress is your best option for creating websites

Hi everybody!

Here I’m again to write my first post in 2015. Last year I’ve wrote less and this year I have plans to write much more, telling about my experience with web development. I have plans to start screencasting too, but this is just plans 😀

I decided to start this year writing about WordPress. Last year I thought WordPress helped me and my company to develop websites faster with a bunch of nice features, making a better job and with less time I spent before.

So, today I’m starting a series about using WordPress to create websites. First I’m going to write about why you should use WordPress to develop your next website and after how you can do that, which plugins you can use and how to use them, one by one.

Continue reading Why WordPress is your best option for creating websites