Tag Archives: laravel tutorials for beginners
Laravel Join 3 Tables
Laravel Join 3 Tables: Joining more than two tables is very common in any framework, we often need to join 3 or more then three table in Laravel. Here in this tutorial we are going to explain how you can join three tables or more than three tables in Laravel.
Laravel Join 3 Tables Example | Join Multiple Tables
It is very simple to join 3 tables in Laravel. Let us create an example to join 3 table β
Laravel Join 3 Tables Example:
$orderList = DB::table('users') ->join('orders', 'users.id', '=', 'orders.user_id') ->join('order_items', 'orders.id', '=', 'order_items.orders_id') ->where('users.id', '=', 5) ->get(); |
The above example will join the three tables β users, orders and order_items on the basis of the foreign keys. You can join multiple tables on the same ways to get the data from different-different tables.
Laravel Check Duplicate Records
Laravel Check Duplicate Records: We often need to check duplicate records when working with database table records. It is very simple to check the duplicate records in Laravel. Here in this tutorial we are going to explain how you can Check duplicate Records in Laravel.
Laravel Check Duplicate Records Example | Laravel check if user exists
Here we have checked that the given email exists in the database table or not. You can simply check the duplicate records in Laravel simply as below-
Laravel Check Duplicate Records Query Example:
first(); if ($userExists === null) { // User Not Found Your Stuffs Goes Here.. } ?> |
The above example will check the given email exists in the database table or not it does not exists then it will return null, Using this response you do other stuffs.
Laravel get Session ID
Laravel get Session ID : Laravel session ID depends upon the version of the Laravel, Here in this tutorial we are going to explain how you can get Session ID in Laravel. We are going to explain how to get Session Id in Different versions of Laravel.
Laravel get Session ID Example
You can get the session Id in different versions of laravel as β
Laravel 3
You can get the Session ID in Laravel 3 simply as below β
Laravel get Session ID Example:
$sessionID = $_COOKIE['laravel_session']; |
Laravel 4.0
You can get the Session ID in Laravel 4.0 simply as below β
Laravel get Session ID Example:
$sessionID = session_id(); |
Laravel 4.1 & Onwards | Laravel 5
You can get the Session ID in Laravel 4.1 and onwards version simply as below β
Laravel get Session ID Example:
$sessionID = Session::getId(); |
Set Headers in Laravel
Set Headers in Laravel: There are many ways you can set headers in Laravel. You can set header directly in view or you can use before filter to bind headers in All views. Here in this tutorial we are going to explain how you can set headers in Laravel.
How to Set Headers in Laravel Example
You can add the headers in your view file directly, here in an example of setting headers in view file β
Set Headers in Laravel Example:
// Outputting a PDF header('Content-Type: application/pdf'); // It will be called my_download.pdf header('Content-Disposition: attachment; filename="my_download.pdf"'); // The PDF source is in original.pdf readfile('my_download.pdf'); |
You can set other headers on the same way in Laravel.
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.
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 β
- 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.