Tag Archives: laravel online course

Laravel create helper


Laravel create helper – Laravel Provides a default helper named as global.php which contains global functions which are available globally. global.php is located at the app/start/global.php location. Here in this tutorial we are going to explain how you can create custom helpers in Laravel.


Laravel create helper

You can create helper in laravel simply as below –

Create a file helpers.php at app/helpers.php location

Laravel create Custom helper Example:


Now you can load this helper in global.php as below –

Laravel load customhelper Example:


Thus you can add your helper functions in this file.

Laravel Encryption


Laravel Encryption – Laravel provides strong facilities for encryption. It provides the AES encryption via the Mcrypt PHP extension. Here in this tutorial we are going to explain the encryption with example.


Laravel Encryption : Encrypt Decrypt Example

Encrypt A Value

You can encrypt a value in laravel as below –

Laravel Encryption Example: Encrypt Value

$encryptedValue = Crypt::encrypt($value);

It will give you the encrypted value.

Note : Set a character 16, 24 or 32 random string in the config/app.php.

Decrypt A Value

You can decrypt a value in laravel as below –

Laravel Encryption Example: Decrypt Value

$decryptedValue = Crypt::decrypt($encryptedValue);

It will give you the decrypted value.

Laravel Errors And Log


Laravel Errors And Log – Laravel provides the some default error and logging system when we start a new project. In addition laravel also provides you monolog library which enables provides various powerful log handlers. Here in this tutorial we are going to cover the laravel error logging system with various example.


Laravel Errors And Log | Create Custom Log File Example

Configuration

  • Error Detail – You can set the error detail configuration in config/app.php file. Go to the line ‘debug’ => env(‘APP_DEBUG’, true) and set it to true if you want to keep the system in debug mode.
  • Log Modes – Laravel supports various types of log modes –
    1. Single – Store logs in single file.
    2. Daily – Create New file daily to store logs.
    3. Syslog – System log.
    You can find the log settings in config/app.php file. By default it is set single.

    'log' => 'single'

    .
    You can change it to daily or syslog also.

Laravel Monolog Configuration-

You can configura Monolog configuration in config/app.php Add the monolog configuration before returning the $app variable.

Laravel Enable Error Logging –

To Enable the error log add the below settings in the config/app.php file-

Laravel enable debug mode Configuration

//'debug' => env('APP_DEBUG', false) Default 
'debug' => env('APP_DEBUG', true)

Laravel Create Custom Logs –

You can crate Custom log in Laravel Simply as Below –

Laravel Create Custom Log File:


The above example will create a log with the information provided.

Here are the following logging levels available-

  • Emergency
    Log::emergency($error);
  • Alert
    Log::alert($error);
  • Critical
    Log::critical($error);
  • Error
    Log::error($error);
  • Warning
    Log::warning($error);
  • Notice
    Log::notice($error);
  • Infor
    Log::info($error);
  • Debug
    Log::debug($error);

Laravel 5 Remove public from URL


Laravel 5 Remove public from URL : We need to remove the public from url in laravel. Here in this tutorial we are going to explain how to remove public from url in laravel.


Laravel 5 Remove public from URL

You can remove the public from url simply as –

  • Step 1: Copy index.php and .htaccess from the public folder –
    Laravel 5 Remove public from URL example
  • Step 2 : Paste these file to the root folder.
  • Step 3 : Now open index.php and change the following lines –
     require __DIR__.'/../bootstrap/autoload.php';

    to

     require __DIR__.'/bootstrap/autoload.php';

    And

    $app = require_once __DIR__.'/../bootstrap/app.php';

    to

    $app = require_once __DIR__.'/bootstrap/app.php';
  • Step 4 : Flush all cache and delete cookie.

Thus the above solution will remove the public from url in laravel

Laravel display flash message on view


Laravel display flash message on view : Session::get(‘message_key’) is used to get the flash message in laravel. In any framework flash messages play a key role to display the messages which makes the error message handling and other messages very easy. In this tutorial we are going to explain how you can handle the flash messages in laravel and display them on view.


Laravel display flash message on view

You can set and get the flash messages as below-

Laravel set flash message : Controller

Laravel display flash message on view:

..
public function myFunction(){
   // your code goes here ..

   Session::flash('success', "Data updated successfully.");
}

The above example will set the flash message in the above controller. Now let us display this flash message on the view file.

Laravel display flash message : View

Laravel display flash message on view:

@if(Session::has('success'))    
        {{ Session::get('success') }}
@endif

The above example will check the flash message and if it has some flash message then it will display on the view file.

Laravel Redirect All Requests To HTTPS


Laravel Redirect All Requests To HTTPS : We sometimes need to redirect all requests to https in laravel. Here in this tutorial we are going to explain how you can redirect all the request to https in Laravel.


Laravel Redirect All Requests To HTTPS

You can use the app/filters.php file and it’s block App::before() check each request if it is not secure redirect it to the https as below –

Laravel Redirect All Requests To HTTPS:

App::before(function($request)
{
    if( ! Request::secure())
    {
        return Redirect::secure(Request::path());
    }
});

The above example will redirect all request to secure https request.

Laravel redirect back


Laravel redirect back : Redirect::back(); is used to redirect back the users from the current url to the back url. Here in this tutorial we are going to explain how you can redirect from current url to back url with errors in laravel.


Laravel redirect back

You can redirect back to the previous url in laravel as below –

Laravel redirect back:

Session::flash('message', "Data saved successfully.");
Redirect::back();

The above example will redirect back to previous url with the given message.

Now to display the message add the following flash message to display it-

Laravel redirect back with message:

@if (Session::has('message'))
   {{ Session::get('message') }}
@endif

The above example will display the message added in the redirect back url.

Laravel select last row from table


Laravel select last row from table : You can get the last row from table by sorting the data in descending order and then get the first row which will give the last row from the table. Here in this example we are going to explain how to get the last row from table.


Laravel select last row from table

You can select the last row from table as below-

Laravel select last row from table:

DB::table('MyTable')->orderBy('ID', 'desc')->first();

The above example will give you the last row from the table Mytable.

If you are using the Eloquent you can select the last record as below-

Laravel select last row from table:

User::orderBy('ID', 'desc')->first();

The above example will fist sort by column ID(auto increment) in descending order and then it will select the first result which will give you the last record from table.

Download files in Laravel


Download files in Laravel : Downloading files from url is very easy in laravel. Response::download($filePath)is used to download files in laravel. This generates a response which forces users browser to download the file from the specified path. Here in this tutorial we are going to explain the download files.


Download files in Laravel

Here in syntax for downloading files in laravel.

Syntax

Syntax for Downloading files in Laravel 5 from url:

return response()->download($pathToFile, $name, $headers);
  • $pathToFile : Full Path of file to be downloaded.
  • $name : Name is optional. This is name which user will see after downloading the file.
  • $headers : Headers are also optional. You can pass an array of HTTP headers.

Example

Here is a simple function which contains the code to download files in laravel using the Response::download() .-

Download files in Laravel 5 from url Example

public function fileDownload(){
        //Suppose profile.docx file is stored under project/public/download/profile.docx
        $file= public_path(). "/download/profile.docx";
        $headers = array(
              'Content-Type: application/octet-stream',
            );
        return Response::download($file, 'my-filename.docx', $headers);
}

The above example will download the file with name my-filename.docx on user’s machine. Both second and third parameters are optional.