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

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)

Working with Laravel 4 or 5 and WordPress together

Hi everybody!

Updated Mar 3rd 2015: Are you using Laravel 5? Check these changes!

Updated May 11th 2014: Using Corcel project

Currently I am working on a project where I had to make some choices about technologies and how work with them together. First this project will be developed using WordPress only. It’s a College Group site, where we had to work with 13 schools around the world and each one must has the control of your own content.

This could be made with WordPress, but I think when the site is not so small maybe you can use another CMS or Framework, because I particularly prefer to work with MVC. So because some decisions inside the company we decided to use WordPress Admin Panel, that is a very good, use its architecture and its database. So WordPress will be used to the application back-end, with user control, user permission, etc.

To the front-end we decided to work with Laravel. To query information from the WordPress database we’ve used the WordPress functions inside Laravel, so it’s much better to work with a MVC WordPress.

Continue reading Working with Laravel 4 or 5 and WordPress together

Managing assets with Laravel 4

Generally assets are stored in your public directory, right? They are public, so anyone can get access to them. But nowadays the performance is a very important factor when deploying a new app. I strongly recommend you to minify and cache your assets, like CSS files, Javascript files and Images.

If you are using 11 javascript files in your app you don’t have to make 11 requests on the server, one per JS files. You can easily join all files and minify them, so you will have just one minified file. This is easy to do using Laravel 4! I’ve found four Laravel 4 assets managers:

We will work with codesleeve/asset-pipeline package. It’s easy to use and simple to understand.

Continue reading Managing assets with Laravel 4

Working with PHP 5.4+ built-in server with Laravel 4

One of the best improvements of the PHP 5.4 was the built-in web server. Like in Ruby On Rails, now you do not need Apache or Nginx inside your development machine.

To start a web server is easy:

php -S localhost:8000

Continue reading Working with PHP 5.4+ built-in server with Laravel 4

Solve the problem. Just it!

These days I’m thinking about productivity and the use of PHP frameworks. I’ve read some posts about framework X or Y, defending a framework instead of another one.

I know people that use a framework like Zend Framework to develop a simple website just to say they’re using it, and not an “easy” framework. I think you must to solve the problem, not create another one. You have to use best practices but first you have to solve the problem, nothing more.

You don’t have to use a hard-to-learn framework just to say your friends you know about it. You can do amazing things with easy-to-learn frameworks, like Code Igniter, CakePHP, Laravel or Slim. You can do better even with pure PHP if you want, but you have to concern about productivity (and security of course).

Continue reading Solve the problem. Just it!

Why you should use Composer and how to start using it

Hello!

One of the most big changes happening in PHP world is the Composer. I’m shure you heard about it but maybe you don’t know why you should use it and how much it is good for you and your projects.

What is Composer?

When you need some specific code in PHP you can go to PHP Classes and search for what you need. For example, you need a class to connect to your Twitter account and get the last updates in your timeline. Ok, you’ll find it easy. And what you have to do after? You download the classe and copy to some place inside your project, and call it including or requiring it inside your PHP code, right?

Well, you can use Pear to. You must install it on your server and install some components that you can use like a package manager. This work too but nothing is installed inside your project structure, but in your machine.

Ok. Composer came to solve all this questions. Together with Composer we have some rules, making all PHP project with the same structure, using namespaces, the same code style, etc. This simplifies when you have to include some third-party code or project. This rules you can find at PHP Fig (PHP Framework Interop Group). I really suggest you to take a look at phptherightway.com. There you’ll find a lot of information to made you a good PHP developer using what we have of better in PHP world.

Continue reading Why you should use Composer and how to start using it

Why you should use a PHP Framework and why I’m using Laravel

Hello everyone!

Use or not to use a PHP Framework on your new projects? Here I’ll talk about my personal opinion about that and I wish help you.

First, I have a simple concept about languages and respectives frameworks: a framework must make the development simpler. Every software has a single purpose: work! This is the main goal of every project, do that to simplify the life of someone.

Talking about PHP, it is a very simple language. It was projected to be simple and fast. If you want a language complicated or filled of dependencies, PHP is not your best choice.

PHP has the power to do a lot of things in a single line, so, if your framework of choice do the same thing in 3 or 4 lines, use the native PHP way. But you’ll ask me your framework do the same job but much better, using OO, or with beautiful code. Remember you must do the thing and make your project work. Of course the OO programming is the recommended way and you must use it, to make you like easier.

What I want to say is you should use the simpler way, to make the development faster and better. Choose a framework that help you to do the job faster, and use PHP for that. Exists some frameworks that you must learn another language almost. For me a framework must be easy to learn and with minutes you will be developing using it and make money.

Continue reading Why you should use a PHP Framework and why I’m using Laravel