Page 2 of 4

Eloquent trick #1: Replace conditionals with “when”

It happens very often that we want to apply certain eloquent query conditions based on what a request sends in. Sometimes it’s a “search by name” thing, other times we just need to filter the records based on a status column.

Usually it looks like this:

public function index(Request $request) 
{
    $posts = Post::newQuery();

    if ($request->term) {
        $posts->where('name', 'LIKE', "%{$request->term}%");
    }

    if ($request->status) {
        $posts->where('status', $request->status);
    }

    return view('posts.index', ['posts' => $posts->paginate()])
}

I recently discovered there’s an alternative to using conditionals. The “when” method executes a callback (second parameter) when the first parameter evaluates to true.

public function index(Request $request) 
{
    $posts = Post::when($request->term, function($q, $term) {
        $q->where('name', 'LIKE', "%{$term}%");
    })->when($request->status, function($q, $status) {
        $q->where('status', $status);
    });

    return view('posts.index', ['posts' => $posts->paginate()])
}

It’s not all that better if you ask me. Yes, it hides the conditionals, but it also makes the code harder to read. Especially if you have more conditions to add.

I’d use this “when” approach for single conditions only.

public function index(Request $request) 
{
    $posts = Post::when($request->term, function($q, $term) {
        $q->where('name', 'LIKE', "%{$term}%");
    });

    return view('posts.index', ['posts' => $posts->paginate()])
}

Thank you, apple, for letting us know we’re not alone

Feel bad for messing up something at work lately? You’re not alone :). Apple messed up big time.

The bug lets you call anyone with FaceTime, and immediately hear the audio coming from their phone — before the person on the other end has accepted or rejected the incoming call.

Read more here.

Amend git user information for all commits

It happened way too many times to push commits without realizing the author information was wrong. To fix it, I used to rebase, mark each commit to edit, and set the correct information.

Today I found out there is a better way.

The command bellow rewrites the entire commit history, updating the user name and email on all branches and tags.

Important: only run this if you’re the only committer of the repository.

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Bazinga! Enough is enough.

I’ve followed the Big Bang Theory series up until season 5 or 6. Can’t really remember when they got too boring for me.

Today I learned Jim Parsons decided to quit, causing the show to close shop. Everyone in the cast wanted to continue, but Jim said enough is enough. It’s time to wrap this 12-year show and go do something else.

Apparently, he’s been getting a lot of flack. He got his fame, money, and power, and ruined what was still considered a good show. He doesn’t care about others losing their job.

The best things in life are free. The second best things are very, very expensive .

Coco Chanel

What people fail to understand is, once you’re past a certain threshold, money no longer bring you the same satisfaction.

And while it’s true that many people will lose their job, it’s not like they weren’t expecting it. They knew the end was near. It’s super rare for a show to go on for twelve years.

Can you imagine to be working on the same thing 12 years in a row? An author writing on the same book, a musician working on the same album, a painter painting on the same picture. Year after year, for more than a decade. That’s crazy.

Yes, he (and others) got his money and now he wants out. And that’s ok.

My cowboy days are over

I did an oopsie today. It wasn’t bad. It was really, really bad.

I was trying to figure out a “it doesn’t work on my end” problem on the live server and I accidentally ran a piece of code that blocked a few thousands of users from accessing the platform.

Luckily, we had backups in place and managed to reverse the changes. Everything is fine now.

Quick reminder.

You might think that it is a small change. That it would only affect a tiny segment of your system. A single user. Just for a few moments to check your theory. But there are no small changes when they are made directly on the live server.

My cowboy days are over. No matter the urgency of the request.

Remember what makes winter so great

It is only natural, as the years run by, for our priorities to change.

We are no longer ditching, or hurrying homework to sneak outside and play. We are no longer concerned our sleigh is not fast enough. Or to have the biggest, fattest snowman in the whole neighborhood.

But this winter we rolled the years back.

Continue reading

Refunds and customer care

When you buy clothes as a present, even if you know the exact size, there is a good chance it won’t fit the person you’re buying for. But you’re willing to take that risk because most stores allow you to exchange the item for a different size, and sometimes even offer a full refund.

We nailed all presents this Christmas, except one; a shirt. It was all we wanted: soft, simple, dark, and elegant. But it just wouldn’t fit well on the person we bought it for. The size was right, but it was to tight on the shoulders.

We went back to the store and asked for a refund. The experience was awful. It’s not that they didn’t accept returns, or that we had to sign ten different documents, or that we didn’t get a full refund. Every time we went shopping there they were smiling, greeting, being helpful. But not this time. Everybody has their ups and downs, but man, the look on her face when we said we needed to return something. She made us feel like we’re stealing her kids’ food of the table.

A few years ago, when I was still a student, I went shopping for groceries at the local supermarket. When I got to the counter and swiped my card, the cashier said the payment wasn’t processed and that I have to swipe again. Once I was out, I went to the first ATM and checked my credit card. I had two transactions, seconds apart, for the same amount.

Five minutes later, I was back in the store talking with the store manager, explaining the situation and showing him my credit card statement. He took my name and phone number, and assured me I will get a refund.

After 15 days I got my money back. 15 days is a hell of a long time for a student with “student money”. I never stepped foot in that supermarket.

Your goal as a service/products provider is to make every experience pleasant, not only when you get to profit. Exchanging sizes, refunding, unsubscribing or canceling your account need to be as painless as possible.

Grammarly, why no trial?

I’ve been a Grammarly user for a few months now and it’s been immensely helpful. I don’t think I made another typo since I start using it. It’s the best spellchecker on the market.

It also has a premium version which promises tasty features like advanced checks for grammar, context and sentence structure, vocabulary enhancement suggestions, genre-specific writing style checks, and even a plagiarism detector.

All great things worth paying for. But something doesn’t feel right when looking at their pricing table. There’s too much of a difference between the monthly and the annual plan.

Grammarly premium pricing plans

I get the idea of enticing people to buy the yearly plan by offering a great deal, but pricing the monthly plan so high makes it look suspicious. Like you really don’t want people to go and try it for 30 days.

A premium trial of even one day, or one document, would go a long way in helping people decide if they want to upgrade or not.

Sincere apologies work

A few weeks ago, a couple of friends and I made plans to get away and spend the new year’s eve together. We searched for a location, booked a small country house, and made a list with everything we need to buy.

Almost everybody in the group drinks wine. White or Rose. Dry. Some of us prefer a specific brand, and with Black Friday being around, we found a great deal and ordered a few bottles.

A week passed and no courier called to deliver the goods, but then again, it was Black Friday season, so long delivery waits were to be expected.

The second week or so, I received an email saying it could take up to 4 additional weeks to deliver the order. They also stressed they’re really sorry for the inconvenience, assured me these delays only happen during Black Friday period, and that they will completely understand if I were to cancel the order.

Their honesty impressed me. Most stores would keep quiet and look away. I thanked them and said it’s OK as long as the order gets delivered until the 21st of December, leaving me enough time to shop around for alternatives.

I used to read about how saying sorry and saying it right can do you more good than any marketing trick, but I never got to experience it on my own skin. It fucking works. I like them better now.

Jose, it’s over.

A Jose Mourinho team cares about the result and the result only. Not a single team, including R.Madrid, has ever played truly attacking football under Jose. Defend well, and maybe snatch a goal on the counter. Or on the next set piece. There’s no room for flashy tricks and flicks in a Jose Mourinho team.

Countless trophies, two UCLs, four league titles in four different countries are proof that a defensive game can bring you results. It worked at Porto, it worked at Chelsea, at Inter, and at Chelsea again.

It didn’t work at Manchester United because Jose failed to build the relationships he used to have with Terry, Lampard, Drogba, Zlatan, Materazzi.

Lots of today’s players claim they would run through a wall for their managers. Will they?

A defensive team puts in a lot of effort. Players put the team’s interests above their own. Strikers play as defenders, defenders play as strikers.

Most players today lack that humility. It isn’t enough to be part of the team, they want to be the team. Money and fame in football are on a whole different level. One good flick, 100k likes. Who cares about winning when you can just dab once in a while?

Jose, the age of warriors is over. They don’t make them like they used to.

© 2019 cdruc

Theme by Anders NorénUp ↑