Laravel, Eloquent: SQL query with left join

If the query needs parentheses/brackets for a where condition like below Normal SQL:

SELECT users.fname, planets.name 
FROM  users
LEFT JOIN  planets ON (users.planet_id = planets.id)
WHERE users.lname LIKE '%hawk%' 

ELOQUENT SQL:

$name = "hawk";

$userResults = DB::table('users')
       ->leftJoin('planets', function($join) {
         $join->on('users.planet_id', '=' , 'planets.id');
        })
       ->where('users.lname' , 'like', '%'.$name.'%')
       ->get(array('users.fname', 'planets.name'));

Eloquent: SQL Query with where or in brackets

If the query needs parentheses/brackets for a where condition like below Normal SQL:

SELECT * 
FROM  users
WHERE planet = 'earth'
AND (fname LIKE '%hawk%' OR lname LIKE '%hawk%')
AND state = 'FL'

ELOQUENT SQL:

$name = "hawk";

$usersResults = DB::table('users')
    ->where('planet', '=', 'earth')
    ->where(function($query) use ($name){
            $query->where('fname' , 'like', '%'.$name.'%');
            $query->orWhere('lname' , 'like', '%'.$name.'%');
      })
     ->where('state', '=', 'FL')
    ->get();

Wordpress: Print-friendly, Print as it looks in browser

If you you want to print a wordpress blog/site as it looks on the browser, then you have to
1. Login to the Wordpress as admin
2. Goto Appearance->Editor in the Left Sidebar menu.
3. On the right side find Header.php file
4. In the file locate the following piece of code

<link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_url’); ?>” type=”text/css” media=”screen” />

5. Modify to the media=""screen" to media="screen, print"
6. Click Update file at the bottom to save the changes in header.php

<link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_url’); ?>” type=”text/css” media=”screen, print” />


Oracle: Split a String Based on Delimiter

We can split a string based on Delimiter using a combination of  INSTR and SUBSTR functions:

INSTR Returns the position of a String within a String. For more information see Oracle instr function
SUBSTR Returns a substring. For more information see Oracle substring
Syntax of SUBSTR:

SUBSTR([input],[start],[length])  
Example 1:
 
select substr('orange.apple',1,(instr('orange.apple','.')) - 1)  
 from  dual  

Output:
first

Example 2:
 
select substr('orange.apple',1,(instr('orange.apple,'.')) - 1) as First,  
        substr('orange.apple', (instr('orange.apple','.')) + 1) as Second  
 from  dual 

Output:
First         Second
orange           apple

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...