Monthly Archives: December 2018

Remove public URL on shared hosting | Laravel

Laravel out of the oven is not prepared for being deployed on shared hosting.

I often use shared hosting for testing web apps as it’s the easiest and fastest way of having an app running in WWW.
See bellow what changes are required in order to make Laravel work on shared hosting.

First step is to complete the Laravel setup. You’ll notice that you can access the app by placing /public/ in the URL. This is obviously not fancy at all.

Fix it by replacing the content of .htaccess in the root directory with the code bellow:

<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteCond %{REQUEST_URI} !^public
	RewriteRule ^(.*)$ public/$1 [L]

That’s all. Remove the /public/ and refresh.

Check my other Laravel posts.

Convert seconds to Days, Hours, Minutes, Seconds

Learn how to convert number of seconds into days, minutes, hours and seconds.

That’s it. You can now create a countdown timer, display the difference between two dates or just print  the time until something great happens.

var seconds = 1001110 // 11 days 14:05:10

// compute the number of days
var days = Math.floor(seconds / (3600*24))
seconds -= days*3600*24

// compute the number of hours
var hours = Math.floor(seconds / 3600)
seconds -= hours*3600

// compute the number of minutes
var minutes = Math.floor(seconds / 60)
seconds -= minutes*60

You now have 4 variables with values as follows:

days === 11
hours === 14
minutes === 5
seconds === 10

Bonus! Learn how to pad the values with 0 so that they look pretty 😀

function str_pad_left (string, pad, length) {
     return (new Array(length+1).join(pad)+string).slice(-length)

console.log(str_pad_left(minutes, '0', 2)) // "05"

Check this article to learn how to create a countdown timer.

How to build dynamic query in Laravel

Dynamic query allows us to start with a base query and extend it so that we properly filter our data. You probably already used and know what Laravel is and how to create a basic database query.

We’ll cut straight to the problem:

How do we dynamically apply query conditions in Laravel?

Let’s take a look at the following example that I consider quite easy and basic query on a Listing model that has title, description, price, images and date columns:

$title = "planet";
$listings = Listing::where(function($q) use ($title) {
$q->where('title', 'like', $title .'%');

This query will return all our documents for which the title has the form planet% (they start with planet word).

You already knew how to achieve this so let’s extend this to a condition based query generation. Follow bellow:

$title = 'planet;
$price_min = 10;
$price_max = 100;
$listings = Listing::select('title', 'description', 'price', 'images', 'date');

// we're only going to filter by title if it's available
if(isset($title) && !empty($title)) {
$listings->where(function($q) use ($title) {
$q->where('title', 'like', $title.'%');

// but there's more
// what about the price?
if(is_numeric($price_min)) {
$listings->where('price', '>=', $price_min);

We’ve now learned how to build a dynamic query and how to add conditions on the run.

So how do we view our results? 1 line:

$listings = $listings->latest()->get();

That’s it. Now go and build you great app!

Google Ads placement In WordPress Sidebar

Google AdSense Ad Units options

Writing articles and maintaining a website takes some time and is not exactly free. If you want to start monetizing your WordPress website you could use Google AdSense and place Google Ads in your website or mobile app.

There are a few steps until you can manage to show ads on your websites. Let’s get started:

AdSense Account Activation

First thing we need is to create a Google AdSense account. This is the place from where we obtain our Google Ads. Start by signing into and init the account activation process. This usually takes between 3-10hours but it depends on your account and website.

You’ll be sent an approval email so soon as you receive it you’re good to go.

Ad Unit Creation

Google AdSense Menu

Head to AdSense webpage again and hit Ads->Ad units.

Each zone of the website where any ad will appear should have a dedicated ad unit.

We’re going to create an ad for the sidebar so choose Text & display ads. Enter an unique id for your add which can be something like WEBSITE_AD_SIDEBAR then click save.

You can now obtain the Ad Code which must be added into your website.

Placing Ads On Website Sidebar

How to create HTML Text Widget

Head to your Worpress Admin Menu.

Enter Appearance->Widgets.

Drag a new Custom HTML to your siderbar and fill the content with the exact Ad Code you get from AdSense.

Click Save!

And Voila, you know have Ads on your WordPress Sidebar.