How to add case insensitive where in Laravel
Have you ever found yourself writing the same complex database queries repeatedly? Yeah, me too. It's frustrating, time-consuming, and, let's face it, not the best use of our coding superpowers.
I needed to search some tables for case-insensitive values and discovered that Laravel doesn't have a native function in eloquent, or it's entirely possible that I missed it. Today, we will tackle this problem head-on by diving into the world of Laravel Eloquent macros. If you've never used them, this will be a game-changer for your productivity.
Picture this: You're working on a project where you frequently need to perform case-insensitive searches across multiple columns. Or maybe you're constantly writing complex ordering logic to handle null values. Sound familiar?
If you're nodding your head, you're not alone. This is what I call "Query Repetition Syndrome," and it's a common ailment among Laravel developers.
Enter Eloquent macros - your new secret weapon against repetitive queries. These bad boys allow you to extend Eloquent's query builder with custom methods. It's like giving your Laravel app query superpowers!
Let's break down how to implement this solution step-by-step.
First things first, we need a place to house our macros. Let's create a new service provider:
Open up that freshly minted MacroServiceProvider.php, and let's give it some structure:
Let's tackle that case-insensitive search problem with a whereInLike macro:
While we're at it, let's add a couple more handy macros:
Don't forget to tell Laravel about your new macros. Add this line to your config/app.php:
Now for the fun part - using your new macros:
By implementing these Eloquent macros, we've solved our Query Repetition Syndrome. Instead of writing the same complex queries over and over, we have reusable, eloquent (pun intended) methods that make our code cleaner and more efficient.
But here's the kicker - this is just the beginning. You can create macros for any repetitive query pattern in your projects. The possibilities are endless!
Eloquent macros are like a Swiss Army knife for your database queries. They're powerful and flexible and can save you tons of time and headaches.
Remember, the goal here is to work smarter, not harder. By identifying repetitive patterns in your queries and turning them into macros, you're setting yourself up for cleaner, more maintainable code in the long run.
Now, I'm curious - what repetitive query patterns have you encountered in your Laravel projects? Please drop a comment below to share your macros or share some macro solutions!
Happy coding, and may your queries be ever elegant!
Deploying Storybook via Gitlab Pipeline via SFTP
This is an update to my previous script to deploy Storybook to S3 via Gitlab Pipelines. We're running a dev server on EC2 and I wanted to be able to deploy the project using SFTP. This assumes you already created a new user account on the FTP server.
Create an SSH key on your local machine and save it without a password using the following command:
You should have two new files in .ssh directory:
Copy content of private key and go back to GitLab project. Navigate to Settings -> CI/CD -> Variables -> Expand -> Add Variable. GitLabโs variable is a key-value pair. Name key SSH_PRIVATE_KEY and paste private key in value field. Click Add Variable.
Add two more variables:
Copy the contents of public key and go back to remote server. Login as the same user which you have specified inย SSH_USERย GitLabโs variable.
Navigate to /home/<username>/.ssh. If directory .ssh doesnโt exist, then create it. Paste the public key into authorized_keys file. If you donโt have authorized_keys file, create it.
Important Note: I ended up using node:16 for my script because my instance of Storybook was using Webpack 4 and the node:latest was using Node 18 which wasn't compatible. If you're using webpack 5, you should be able to use a later version. I'm also using node:16 instead of alpine or buster because I ran into issues adding the SSH keys. I'll work on optimizing this later for performance but it works.