Can’t install extensions on VSCode?

If you are trying to install extensions on VSCode but you can’t. Try to visit this site from your browser:

http://cdn.vsassets.io/

Most probably you will get a “This site can’t be reached” message, to solve this you have to change your default DNS to google free public DNS

8.8.8.8
8.8.4.4

If that may not work for you then yo can try to change the DNS for IPv6 to:

2001:4860:4860::8888
2001:4860:4860::8844

Here is a link for more details on changing the DNS to google DNS

https://developers.google.com/speed/public-dns/docs/using

How to install composer on Godaddy cPanel

If you want to install composer on a godaddy shared hosting, you can follow these steps:

On root folder type:

wget https://getcomposer.org/installer
php installer --check
php installer
rm -f installer

to return the path of composer type:

which composer

it’ll return something like: /opt/cpanel/composer/bin/composer

then you can use composer using its full path:
php-cli /opt/cpanel/composer/bin/composer install

for ex. :

php-cli /opt/cpanel/composer/bin/composer install
php-cli /opt/cpanel/composer/bin/composer update

Also if you want to run php artisan , you can type:

/usr/bin/php-cli artisan

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 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;