CyReX
CyReX

Reputation: 1576

Proper SQL to Laravel ORM

I am newbie on Laravel, I have a proper SQL Statement (which works on PHPMyAdmin and Navicat) and I can get results. What i want to do is, I want to take that statements in Laravel without using DB::raw();.

Any Helps will be appreciated.

    Select 
        rmm.message,
        count(rmm.message) as number, 
        receivedTime as time

    FROM
           rcs rcs, rmm rmm
    WHERE
          rmm.smsCid =  rcs.smsCid AND  rmm.receivedTime LIKE '%2013-04-01%' AND length('rmm.message') > '3'

    GROUP BY(rmm.message)

Upvotes: 2

Views: 1160

Answers (2)

juco
juco

Reputation: 6342

Alternatively you can use the Fluent Query Builder. Here's my attempt, of course it's not checked as I don't have your data structure to hand. Hopefully enough to get you started:

DB::table('rcs') // SELECT FROM rcs
    ->join('rmm', 'rmm.smsCid', '=', 'rcs.smsCid') // Simple INNER join
    ->where('rmm.receivedTime', 'LIKE', '%2013-04-01%')
    ->where(DB::raw('LENGTH(rmm.message)'), '>', 3)
    ->get(array(DB::raw('COUNT(rmm.message)'), 'rmm.message', 'receivedTime')); // Get only the columns you want

Upvotes: 4

Robert K
Robert K

Reputation: 30328

You can use DB::query() to run your query. This calls the query method on the active DB connection.

Upvotes: 2

Related Questions