LaravelPDO

How to add PDO in laravel?

Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to add PDO in laravel? I am doing with laravel first time in my website. Laravel is one of the top php mvc framework.
PDO is oops based and more securable then mysql and mysqli.

Here are the way we can add pdo in laravel blade template file and In laravel controllers files:

1. PDO call laravel blade template files:
$PDO = \DB::connection()->getPdo();
$QUERY = $PDO->prepare(“SELECT DISTINCT * FROM users”);
$QUERY->execute();
$users=$QUERY->fetchAll(PDO::FETCH_OBJ);

2. PDO call laravel Controllers files:
private $PDO;
/**
* Construct create PDO instance
*/
public function __construct()
{
$this->PDO = DB::connection()->getPdo();
}
public function index()
{
$QUERY = $this->PDO->prepare(“SELECT DISTINCT * FROM user”);
$QUERY->execute();
$user = $QUERY->fetchAll(\PDO::FETCH_OBJ);
return view(‘user.index’, compact(‘user ‘));
}

There are so many code tricks in laravel and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Positive SSL