megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Clearing cache on a Shared Hosting Server

Hosted your website on a Shared Hosting Server and got limited access to clear the cache on your project?

Open up routes/web.php and create this route:

<?php
Route::get('/clearcache', function(){
    \Artisan::run('config:clear');
    \Artisan::run('cache:clear');
    \Artisan::run('view:clear');
    \Artisan::run('route:clear');
    \Artisan::run('config:cache');
});
?>

Just type the URL and it will clear all existing cache from the project.

Hope this helps you out!

Convert numbers from English to Arabic in PHP

If you're a developer working in the Middle East, it's quite common that you'll work on a project that bilingual, in our case, it's english and arabic.

In my opinion, it's not aesthetically pleasing and logical to have english numbers in arabic text, so, write a simple helper function to convert the numerals from english to arabic:

<?php
function convertEnglishToArabicNumerals($str) {
    if (\App::getLocale() == 'ar') {
        $westernArabic = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        $easternArabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
        $str = str_replace($westernArabic, $easternArabic, $str);
    }
    return $str;
}
?>

And since most browsers can handle RTL, you don't have to worry about how the arabic numerals are being displayed in your application.

Convert string to variable in PHP

I read about variable variables in PHP's official documentation.

Here's a sample:

<?php
    $a = "hello";
    // Remove special characters and tags to prevent it from crashing.
    $foo = preg_replace('/[^a-zA-Z0-9\s]/', '', $$a);
    echo $foo;
?>

Not sure if this is a good practice but it sure gets the job done!

How to resolve the "Failed to clear cache. Make sure you have the appropriate permissions." error

This error is annoying and mostly happens if the data directory is missing under the storage/framework/cache/data directory. For some reason, this folder doesn't exist by default.

To resolve it, just manually create the data directory under the storage/framework/cache/data directory and it should fix the issue.

How to exclude certain slugs in Laravel

Using plain Regular Expressions, you can exclude certain slug from your routes, try adding the following to your routes/web.php file:

<?php
    Route::match(array('GET', 'POST'), '/{slug}', 'YourController@index')->name('page')->where('slug', '^(?!pattern).*$');
?>

Hope you found this useful!

Convert to date from timestamp using Carbon

Using Carbon's createFromFormat() method is basically a wrapper for DateTime::createFromFormat(), the main difference between the two methods is that you can add a timezone to Carbon's method.

Here's a sample on how you can convert to date using timestamp using Carbon:

<?php
    function formatDate(Request $request) {
        return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $request->date)->format('Y-m-d');
    }
?>

Hope you found this useful!

Convert string to date using MySQL

Today, I was debugging a piece of code that is supposed to return a list of data based on the year, the funny thing is that the data was being returned on the development server but not on the production one.

Weird, so I opened up MySQL Workbench and wrote a simple query to see if the dates were being returned because who knows maybe they weren't stored at all.

SELECT YEAR(date_posted) FROM posts;

The values returned were null. Now, that's strange because the dates were present in the column. So, I took a deep look and figured out that the dates were stored in VARCHAR instead of DATETIME data type! 😔

Luckily, I figured that there's a way to resolve this by STR_TO_DATE() function:

SELECT YEAR(STR_TO_DATE(date_posted, '%Y-%m-%d')) FROM posts;

Bam! The results were coming now! 😌

Hope this helps you out!

Show Git branch in your Bash prompt (with colors)

Do you work on a project with multiple Git branches but don't know which one you're in? Open your .bashrc file and add this:

force_color_prompt=yes
color_prompt=yes

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

unset color_prompt force_color_prompt

Updated: November 26th, 2022

The above script works fine for Ubuntu but doesn't work fine on other distros. Here's an alternative one that works on all distros:
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "

Save the file and execute this command for your changes to take effect:

source ~/.bashrc

Now, you should see your colors in your Bash prompt along with the Git branch that you're working on (Note: this will be shown if you're in a project that uses a Git repository).

Hope this helps you out!