Friday, June 3, 2016

How to Make a PDF from View in Laravel? | How to generate pdf using blade view with dompdf in laravel?


laravel pdf download


In this session we gonna a make a very cool thing. Sometimes we need to generate Pdf for creating report or to show data into document . So today we are gonna convert a static view page to pdf using Laravel.

Let's start!

1. In the first process we gonna install a package called /laravel-dompdf
It's very easy to install in your project. Just go through the doc and have installed it.
If you can't install in your project, Just ping me in comment.

2. Then create a route in Route.php

Route::get('/pdf', [
       'uses' => 'courseController@createPdf',
        'as' => 'pdf'
    ]);

3. Then create a Controller :

public function createPdf()
{
        $alldata=Course::all(); // it'll load all the data from course model.
        $pdf = \PDF::loadView(
'invoice', compact('alldata'))->setPaper('a4')->setOrientation('portrait');
return $pdf->download('invoice.pdf');       
}

And don't forget to use namespace above the the controller otherwise it will show Error.

namespace we have to use:

use Barryvdh\DomPDF\Facade as PDF;

4. In controller you save seen that in that loadView('invoice')  (It's actually a view page which will generate the PDF). So we have to create first this view page where we can show all the data what a user wanted to see.

5. That's it so when the user click on that pdf route, it will automatically generate/ convert the page into PDF.

That's it for today. Hope you liked it. If you want me to post more tutorial on laravel don't forget to comment or feedback.

Thank you.

Thursday, June 2, 2016

How to remove /public/ from URL in Laravel Project?






remove public from url


In this instructional exercise we will figure out how to remove public from laravel url.You can utilize this technique for laravel5.X+ and lower versions.By default, the URL which accompanies Laravel App will be this http://example.com/pubic/.So to get perfect URLs which are useful for SEO reason you should remove public from url in laravel application.

It's very easy for laravel 5+  .Just do the following process:

1. Rename the server.php in the your Laravel root folder to index.php


2. Copy the .htaccess file from /public directory to your Laravel root folder.

That's it. Just Reload the Project. You will see the project url without /public in it. 
Let's see with a demo video Tutorial.

 

Wednesday, June 1, 2016

PHP Laravel : How to logout a user from all devices using Redis?

laravel redis



Here is a common scenario like after password reset I/Admin want to logout the user from all device except the device which he logged in while password reset or suppose a user have logged in any many device and the admin want to logout from all of the devices for security purpose for certain reason.

In laravel we can finish the task very easy way using Redis. Lets solve the issue with the following steps.

1. First install the Predis package by composer:

In composer.json file add the following line and run update composer in your cmd.

"require": {
            ...... 
            "predis/predis": "~1.0" ,
            ...... },


2. Create a Session table in database using the following commands:

php artisan session:table
composer dump-autoload 
php artisan migrate

3. Then Download Redis in your PC from the below link and install it in your C:// drive in program files folder.


4. After Redis installed run redis-server.exe file means your server is ready. Now if you run your application.

5. Don't forget to change your .env setting for following driver :

CACHE_DRIVER=redis
SESSION_DRIVER=redis


5. Here is a sample controller code I have shared for logged in using Redis as well as logged out.


public function postSignIn(Request $request)
 { 
     if (Auth::attempt(['email' => $request['email'], 'password' =>$request['password'] ]) ) { 
    //for save session ID I use (when user login )
     $redis = \Redis::connection(); 
     $userId = Auth::user()->id;
     $redis->sadd('users:sessions:' . $userId,Session::getId()); 
     return redirect()->route('main');
   }
  return redirect()->back();
 }

 public function getLogout()
 {
   $redis = \Redis::connection();
   $userId=Auth::user()->id;
   //get all session IDs for user
   $userSessions = $redis->smembers('users:sessions:' . $userId);
   $currentSession = Session::getId();
   //for logout from all devices use loop
     foreach ($userSessions as $sessionId) {
       if ($currentSession == $sessionId) {
       continue; 
     }
   //for remove sessions ID from array of user sessions (if user logout or manually logout )
   $redis->srem('users:sessions:' . $userId, $sessionId);
   //remove Laravel session (logout user from other device)
   $redis->del('laravel:' . $sessionId);
 
   }
  Auth::logout();
  return redirect()->route('main');
 }
6. That's it! When a user logged out from a particular device it will automatically kill all the sessions and logged out from other device also.

How to Add a CkEditor in Laravel Project?

laravel ckeditor



Many of you have asked how can I add a CKEditor in my Laravel Project. So Here is the Very simple solution for that. Let's start.
About CKeditor:

CKEditor (in the past FCKeditor) is an open source WYSIWYG word processor intended to convey basic word processor includes specifically to website pages, disentangling their substance creation. Its center code is composed in JavaScript.

1. First in your view page such as index.blade.php write the textarea where you want to show the CKEditor or form for taking input. Make sure you keep an Id in it so that we can catch it with JavaScript Code:

<textarea id="article-ckeditor" class="form-control" name="description" rows="3"></textarea>

2. Now use the below JavaScript code for catching that Id "article-ckeditor" before the closing tag.

<script src="//cdn.ckeditor.com/4.5.7/standard/ckeditor.js"></script>

 <script>
      CKEDITOR.replace( 'article-ckeditor' ); 
 </script> 

 3. That's it. So if you load the page you will see the CKEditor will appear Instantly in your web page.

That's it for today. Hope you Like the article. Make Sure you Share it.


Sunday, March 20, 2016

How to Install Laravel on Windows?




laravel installation on windows






How to install? It's quite easy so Let's start! 

1. First you need to download  Composer (A dependency manager for php & yes don't forget to check your PC requirement first before downloading).

2. After installing composer in your PC, Run cmd command in your htdocs docs on XAMPP and copy the below line and press enter.

composer create-project --prefer-dist laravel/laravel blog
This will automatically download latest laravel  file in your htdocs forlder.Rename the file as laravel. As we are gonna working in that directory as laravel in different steps.

3. Now press ctrl+shift and click on laravel folder and then click on open command window here.
4. In command window type the following command and run enter.
composer install

5. After successfully installing composer on that folder write the following url on your browser url and press Enter.

localhost/laravel/public
6. You will see a welcome screen like that:

install-laravel-on-windows


7. That's it you have successfully installed laravel on your PC.