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

Lia97's avatar
Level 1

How to convert this Raw query into Laravel Eloquent way?

I'm confused, how to change this query to Eloquent. Can you help me. This is my raw query.

0 likes
5 replies
MichalOravec's avatar

With query builder

DB::table('ARCHIVE_LOG_SHEET_HDR')->selectRaw("ARCHIVE_LOG_SHEET_HDR.MATERIAL_ID as media_no, 'SHOOT' as media_type, '' as media_format, ARCHIVE_LOG_SHEET_PROG01.HOUSE_NO as house_no, ARCHIVE_LOG_SHEET_HDR.LOG_DATE as entry_date, ARCHIVE_LOG_SHEET_PROG01.FILM_NAME as program_name, '' as episode_title, '' as episode_no, '' as last_tx, ARCHIVE_LOG_SHEET_HDR.REMARKS as remarks, ARCHIVE_LOG_SHEET_HDR.LOCATION_ID as shelf_no, ARCHIVE_LOG_SHEET_DTL.REMARKS as short_list")
    ->leftJoin('ARCHIVE_LOG_SHEET_PROG01', 'ARCHIVE_LOG_SHEET_HDR.LOG_ID', '=', 'ARCHIVE_LOG_SHEET_PROG01.LOG_ID')
    ->leftJoin('ARCHIVE_LOG_SHEET_DTL', 'ARCHIVE_LOG_SHEET_HDR.LOG_ID', '=', 'ARCHIVE_LOG_SHEET_DTL.LOG_ID')
    ->get()

Documentation: https://laravel.com/docs/7.x/queries

ahmeddabak's avatar

In order to use Eloquent you need to have a model for each table in the database and you need to define the relations between the tables in the models. but you can use the query builder to achieve similar results.

\Illuminate\Support\Facades\DB::table('ARCHIVE_LOG_SHEET_HDR')
    ->leftJoin('ARCHIVE_LOG_SHEET_PROG01', 'ARCHIVE_LOG_SHEET_HDR.LOG_ID','=','ARCHIVE_LOG_SHEET_PROG01.LOG_ID')
    ->leftJoin('ARCHIVE_LOG_SHEET_DTL', 'ARCHIVE_LOG_SHEET_HDR.LOG_ID','=','ARCHIVE_LOG_SHEET_DTL.LOG_ID')
    ->select([
        'ARCHIVE_LOG_SHEET_HDR.MATERIAL_ID as media_no',
        'SHOOT as media_type',
        '"" as media_format',
        'ARCHIVE_LOG_SHEET_PROG01.HOUSE_NO as house_no',
        'ARCHIVE_LOG_SHEET_HDR.LOG_DATE as entry_date',
        'ARCHIVE_LOG_SHEET_PROG01.FILM_NAME as program_name',
        '"" as episode_title',
        '"" as episode_no',
        '"" as last_tx',
        'ARCHIVE_LOG_SHEET_HDR.REMARKS as remarks',
        'ARCHIVE_LOG_SHEET_HDR.LOCATION_ID as shelf_no',
        'ARCHIVE_LOG_SHEET_DTL.REMARKS as short_list '
    ])->get();

Please note that this is not a working solution, it is just an example of how you might achieve what you asked for.

for more info check the documentation Database: Query Builder

Lia97's avatar
Level 1

Alright, if like that. What can I put it like, does the script below work?

$medias = DB::connection('oracle') ->table('ARCHIVE_LOG_SHEET_HDR') ->selectRaw("ARCHIVE_LOG_SHEET_HDR.MATERIAL_ID as media_no, 'SHOOT' as media_type, '' as media_format, ARCHIVE_LOG_SHEET_PROG01.HOUSE_NO as house_no, ARCHIVE_LOG_SHEET_HDR.LOG_DATE as entry_date, ARCHIVE_LOG_SHEET_PROG01.FILM_NAME as program_name, '' as episode_title, '' as episode_no, '' as last_tx, ARCHIVE_LOG_SHEET_HDR.REMARKS as remarks, ARCHIVE_LOG_SHEET_HDR.LOCATION_ID as shelf_no, ARCHIVE_LOG_SHEET_DTL.REMARKS as short_list") ->leftJoin('ARCHIVE_LOG_SHEET_PROG01', 'ARCHIVE_LOG_SHEET_HDR.LOG_ID', '=', 'ARCHIVE_LOG_SHEET_PROG01.LOG_ID') ->leftJoin('ARCHIVE_LOG_SHEET_DTL', 'ARCHIVE_LOG_SHEET_HDR.LOG_ID', '=', 'ARCHIVE_LOG_SHEET_DTL.LOG_ID') ->get()

But actually I want to use eloquent

Lia97's avatar
Level 1

Maybe, until now honestly I'm still confused distinguish to have to use a query builder or eloquent.

What if I write a script like this..

               $medias = DB::connection('oracle')
                          ->table('ARCHIVE_LOG_SHEET_HDR AS a')
                          ->select('a.LOG_ID', 'a.MATERIAL_ID', 'a.LOG_DATE', 'a.REMARKS', 'a.LOCATION_ID',
                                   'b.FILM_NAME', 'b.HOUSE_NO',
                                   'c.REMARKS as REMARK_DTL')
                          ->leftJoin('ARCHIVE_LOG_SHEET_PROG01 AS b', 'b.LOG_ID', '=', 'a.LOG_ID')
                          ->leftJoin('ARCHIVE_LOG_SHEET_DTL AS c', 'c.LOG_ID', '=', 'a.LOG_ID');

Is it included in the query builder or eloquent?

MichalOravec's avatar

@lia97 Do you have set models and their relationships? If you need select only a few columns from those tables, query builder it's what you exactly need.

Please or to participate in this conversation.