How to read file content in Public folder with Laravel

If you want to read the content of a file in the public folder using Laravel, you can use this code

$file_name = "test.dat";
$file_url = 'public\subfolder\\'. $file_name;
$content = file_get_contents(base_path($map_url));

base_path is a helper function to generate a fully qualified path to a given file relative to the project root directory

How to configure your host in one company and your email in another

If you want to host your website with one company and your emails in another you have to edit your domain name DNS Zone.

All you have to do is to add A host @ to point to your website host and “mail” to point to your email server and finally add MX record @ with priority 0 and Points to mail.yourdomain.com ..

For more details, check this page:

How to solve “gpg: public key decryption failed: Bad passphrase” in batch file

If you are trying to decrypt a file or a bunch of files using batch file in windows you will write something like this:

gpg --pinentry-mode=loopback --batch --yes --passphrase "abc%123" --decrypt-files *.pgp

and you put the above code in a .bat file and run it but you got an error like:

gpg: public key decryption failed: Bad passphrase

You are 1000% sure the passphrase is correct .. well the problem is in the passphrase itself as it contains a special character that needs to be escaped .. in the above example , the passphrase : “abc%123” must be written like this: “abc%%123” as %% is the escape of % … for a complete list of batch file escape characters , check this link

in case if you are wondering about the meaning of
–pinentry-mode=loopback  
it is used to prevent the gui from pooping up and asking for the passphrase.

Another important point , to make the batch option work without problem .. you have to make sure that the encrypted file extensions is *.pgp

Rendering path for HTML web page

Here is the steps the browser takes to render a web page after receiving the content from the server:

  1. Constructing the DOM Tree
  2. Constructing the CSSOM Tree
  3. Running JavaScript
  4. Creating the Render Tree
  5. Generating the Layout
  6. Painting

You can use Google chrome DevTool “Performance” to view rendering in progress with details and charts.

For more details, check this page: 

https://bitsofco.de/understanding-the-critical-rendering-path/

How to view latest SQL statement excuted in Laravel

To view the latest sql query executed in Laravel you can

DB::enableQueryLog();

print_r(DB::getQueryLog());

Note that you have to enable the query log before executing the query and view/print it after execution ..

A good practice is to enable it only in Local environment

if (App::environment('local')) {
    // The environment is local
    DB::enableQueryLog();
}

 

How to sanitize raw data in Laravel

When doing a raw data query from user input like this:

$someVariable = Input::get("some_variable");

$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = '$someVariable'") );

we are at risk of SQL injection , to avoid that we can bin parameters to our query like this:

$someVariable = Input::get("some_variable");

$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = :somevariable"), array(
   'somevariable' => $someVariable,
 ));

 

Another point is that if we want to do a raw that doesn’t return a value, we can do it like this

DB::statement( 'ALTER TABLE HS_Request AUTO_INCREMENT=1111' );

and that way can take parameters as well

DB::statement( 'ALTER TABLE HS_Request AUTO_INCREMENT=:incrementStart', array('incrementStart' => 1111) );

 

How to measure timing in Laravel using debugbar

If you want to measure the timing of some code in Laravel , you can use debugbar measure function

Debugbar::startMeasure('start');

Debugbar::stopMeasure('stop');

but if, for any reason, you can’t use debugbar, you can go back to pure php and use this code

 $start = microtime(true);
 
 $time_elapsed_secs = microtime(true) - $start;