1

As I've understood so far, laravel has some functions which are related to database, and I have to use them to write a query. like these:

  • from()
  • join()
  • where()
  • whereIn()
  • hasOne()
  • hasManey()

Ok well, I'm a newbie in Laravel and honestly I'm confused now. I mean, yes, writing a simple query by functions above is possible, but what about complex queries? how can I use functions above to implement this query:

SELECT count(*) as total_count
       t1.col1,
       t1.col2,
       t2.col1,
       CASE WHERE t1.col4 IS NULL THEN 1 ELSE 'something' END as sth,
       (SELECT col2 FORM table3 WHERE col1 = ?) as related_id
FROM table1 t1
JOIN table2 t2
JOIN t1.col1 = t2.col3
WHERE t1 = ?
GROUP BY t1.co1, t1.col2, t2.col1
ORDER BY t1.col2, t2.col1
LIMIT 1,?

Isn't there any Laravel's method to allow me use pure-mysql codes in it?

stack
  • 10,280
  • 19
  • 65
  • 117
  • Thanks @Drew .. just can please tell me is passing parameters *(prepared statements)* available when I use `DB::select(..)`? – stack Oct 09 '16 at 06:37
  • do you mean [these](http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html) as in prepare / execute / dealloc ? – Drew Oct 09 '16 at 06:38
  • @Drew no, I meant was using PDO .. you know, in general *(when I don't use Laravel)*, I pass parameters to the query by binding them .. but that's true when I use `$db->prepare()` .. now I want to know, how can I pass parameters to the query when I use `DB::select(..)` ? – stack Oct 09 '16 at 06:40
  • well I am no Laravel dev by any means. But raw is a bypass when you want to evade an orm and are ready to go with a more complex query. As such you have your params already baked in. But preferably it would generate a resultset wedged into your "model" – Drew Oct 09 '16 at 06:42
  • So what I would do is start with an orm and use it for very light stuff knowing I could always go "raw" because I am, well, a mysql dev guy afterall. But then I would be wondering why I am using the orm to begin with, and the more I talk here the more I would just bash orms and who wants to hear that. Because orms are great for teams and in my mind simplistic stuff. But when things get hairy, how is it done in an orm without raw? – Drew Oct 09 '16 at 06:45
  • So maybe this [answer](http://stackoverflow.com/a/20873009) or this general [search](http://stackoverflow.com/search?tab=votes&q=%5bmysql%5d%5blaravel%5d%20%20raw%20query) – Drew Oct 09 '16 at 06:54
  • thank you @Drew for the links you provided – stack Oct 09 '16 at 06:58

0 Answers0