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 send variable to wherehas function in Laravel

If you need to send variable to wherehas function , all you have to do it to use “use” .. here is an example…

To list the Users whom their posts are in certain langue .

$language = 3;
$users = Users::whereHas('posts', function ($query) use($language){
                    $query->where('language_id', $language);
                })->get();

Laravel storage can’t be accessed

To be able to access Storage folder, you have to create a link:

php artisan storage:link

In case your are using a shared hosting and can’t access the SSH , then you can run the phpartisan from route or controller

Artisan::call(‘cache:clear’);

Here is an example of running phpartisan from route

Route::get('artisan/command/{key?}', array(function ($key = null) {

        if ($key == "cache - clear") {
            try {
                echo' < br > php artisan cache: clear…';
                Artisan::call('cache: clear');
                echo' < br > php artisan cache: clear completed';
            } catch (Exception $e) {
                Response::make($e– > getMessage(), 500);
            }
        }
        elseif($key == "view - clear") {
            try {
                echo' < br > php artisan view: clear…';
                Artisan::call('view: clear');
                echo' < br > php artisan view: clear completed';

            } catch (Exception $e) {
                Response::make($e– > getMessage(), 500);
            }

        }elseif($key == "link") {
            try {
                echo' < br > php artisan storage link';
                Artisan::call('storage:link');
                echo' < br > php artisan storage:link completed';

            } catch (Exception $e) {
                Response::make($e– > getMessage(), 500);
            }

        }else {
            App::abort(404);
        }

    }));

 

Also you can try running symlink() function

<?php
    $res = symlink('/home/pylonsof/public_html/darwinSource/storage/app/public','/home/pylonsof/public_html/darwin/storage');
    
    echo($res);
    echo('ok');
?><span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>