Home Laravel How to add PDO in laravel?

How to add PDO in laravel?

by therichpost
Published: Updated: 2 comments
Laravel 7.2 routing with route group auth guard check with prefix

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.

Guys if you are new in Laravel 9 the please check below link for Laravel basics information:

Laravel 9 Basics Tutorial for beginners

1. 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.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Thank you.

Therichpost.com

You may also like

2 comments

Indu Bhusan Nath January 19, 2023 - 4:44 am

Dear,

It was a great post. I was looking for PDO queries in Laravel. Could you let me know more?

Reply

Leave a Comment

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