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