Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

behnampmdg3's avatar

Are there any downsides to use raw queries like this instead of query builder?

Are there any downsides to use raw queries like this instead of query builder?

$user_courses = \DB::select('
                SELECT * 
                    FROM table_1
                        INNER JOIN table_2 
                            ON table_1.course_id = table_2.id
                                AND table_2.active = 1
                        INNER JOIN table_3
                            ON table_2.user_id = table_3.id
                                AND table_3.active = 1
                                AND table_2.user_id = ?
                    WHERE table_1.email = ?', [$request->business_id, $request->email]);
0 likes
5 replies
musa11971's avatar

I assume you mean Eloquent?

In my opinion, using Eloquent looks a lot better and is better maintainable. Not to mention you can write extra logic in your model classes, that you otherwise couldn't do.

e.g.:

$courses = User::find(1)->courses;

vs

$courses = DB::select('SELECT * FROM user_course WHERE user_id = 1');

You be the judge.

behnampmdg3's avatar

@MUSA11971 - Thanks for the reply but no this is 100% not what I meant.

Eloquant is very beautiful for simple stuff but relationships can get very tricky with Eloquant.

Try writing this with Eloquant:

$user_courses = \DB::select('
                SELECT * 
                    FROM members
                        INNER JOIN courses 
                            ON members.course_id = courses.id
                                AND courses.active = 1
                        INNER JOIN users
                            ON courses.user_id = users.id
                                AND users.active = 1
                                AND courses.user_id = ?
                    WHERE members.email = ?', [$request->business_id, $request->email]);
CREATE TABLE `members` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `member_course_email_course_id_unique` (`email`,`course_id`)
) ENGINE=InnoDB AUTO_INCREMENT=24904 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `logo` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `login_card_class` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT 'dark',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `active` int(11) DEFAULT '1',
  `logo` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `username` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Cronix's avatar
Cronix
Best Answer
Level 67

No, there's not a downside because you're using query bindings. It's actually faster because laravel doesn't have to translate it back to raw sql to execute it. It's already translated. An ORM or Query Builder will not outperform native sql.

2 likes
behnampmdg3's avatar

@CRONIX - Cronix do you agree sometimes Raw is a better option? I find ORM and Query Builder sometimes limiting. I am sure a lot can be done but the documentation is written very minimal.

Cronix's avatar

The ORM only has like 5% of SQL commands built in so you have to if you want to do anything beyond basic querying. Look at the manual for MySQL or Postgres and compare it to laravels ORM. There are many things you can't do in Eloquent unless you use raw queries because the ORM doesn't have everything implemented. SQL will always be more powerful and faster than any ORM. If you only know the ORM, you are limiting yourself to a very small subset of the SQL language and lose out on a lot of powerful commands. There is no substitute for actually knowing sql.

4 likes

Please or to participate in this conversation.