如何 "translate" 一个复杂的查询到 Laravel 查询生成器?

How to "translate" a complex query to Laravel Query Builder?

我有以下复杂查询,想在 Laravel 中使用它。但直到现在我都没有成功。也许你们中的一个人可以向 Laravel.

的查询构建器展示如何 "translate" 它

这里是 Workbench 中运行良好的原始查询:

select * from lemma l 
inner join etymology e on l.id=e.lemma_id_fk 
inner join gloss g on e.id = g.etymology_id 
inner join wold_meanings w on g.gloss=w.meaning
where g.gloss like '%flower%' #paper, ocean, etc.
limit 100;

比我尝试了以下方法:

  $results = DB::table('lemma')
  ->join('etymology', 'lemma.id', '=', 'etymology.lemma_id_fk')
  ->join('gloss', 'etymology.id', '=', 'gloss.etymology_id')
  ->join('wold_meanings', 'gloss.gloss', '=', 'wold_meanings.meaning')
  ->select(DB::raw('lemma.*'))
  ->where('gloss.gloss', 'like', '%flower%')
  ->get();

还有:

  $results = DB::table('lemma')
  ->select(DB::raw("select * from lemma l 
  inner join etymology e on l.id=e.lemma_id_fk 
  inner join gloss g on e.id = g.etymology_id 
  inner join wold_meanings w on g.gloss=w.meaning
  where g.gloss like '%flower%'
  limit 1"));

还有这个:

  $results = DB::raw("select * from lemma l 
  inner join etymology e on l.id=e.lemma_id_fk 
  inner join gloss g on e.id = g.etymology_id 
  inner join wold_meanings w on g.gloss=w.meaning
  where g.gloss like '%flower%'
  limit 100");

但我完全没有成功... :-(

谁能告诉我正确的方法?我正在使用 Laravel 5.4.

== 编辑 #1 ==

此处应显示结果的视图:

        <table class="table table-bordered table-condensed table-hover table-responsive table-striped" id="table">
            <thead>
                <tr>
                    <th>id</th>
                    <th>lemma_id</th>
                    <th>headword</th>
                    <th>lemma</th>
                    <th>pos</th>
                    <th>gender</th>
                    <th>language</th>
                    <th>origin_family</th>
                    <th>origin</th>
                    <th>short_path</th>
                    <th>origin_path</th>
                    <th>etymology_text</th>
                    <th>first_use</th>
                    <th>lang</th>
                    <th>pageid</th>
                    <th>term</th>
                    <th>non_latin_script</th>
                    <th>lang_2</th>
                    <th>gloss</th>
                    <th>sequence</th>
                    <th>lemma_id_fk</th>
                    <th>derivatives_id</th>
                    <th>etymology_id</th>
                    <th>meaning</th>
                    <th>semantic_category</th>
                    <th>semantic_field</th>
                    <th>simplicity_score</th>
                    <th>age_score</th>
                    <th>borrowed_score</th>
                    <th>description</th>
                    <th>typical_context</th>
                    <th>representation</th>
                    <th>sub_code</th>
                </tr>               
            </thead>

            <tbody>
            @foreach($results as $result)
                <tr>
                    <td>{{$result->id}}</td>
                    <td>{{$result->lemma_id}}</td>
                    <td>{{$result->headword}}</td>
                    <td>{{$result->lemma}}</td>
                    <td>{{$result->pos}}</td>
                    <td>{{$result->gender}}</td>
                    <td>{{$result->language}}</td>
                    <td>{{$result->origin_family}}</td>
                    <td>{{$result->origin}}</td>
                    <td>{{$result->short_path}}</td>
                    <td>{{$result->origin_path}}</td>
                    <td>{{$result->etymology_text}}</td>
                    <td>{{$result->first_use}}</td>
                    <td>{{$result->lang}}</td>
                    <td>{{$result->pageid}}</td>
                    <td>{{$result->term}}</td>
                    <td>{{$result->non_latin_script}}</td>
                    <td>{{$result->lang_2}}</td>
                    <td>{{$result->gloss}}</td>
                    <td>{{$result->sequence}}</td>
                    <td>{{$result->lemma_id_fk}}</td>
                    <td>{{$result->derivatives_id}}</td>
                    <td>{{$result->etymology_id}}</td>
                    <td>{{$result->meaning}}</td>
                    <td>{{$result->semantic_category}}</td>
                    <td>{{$result->semantic_field}}</td>
                    <td>{{$result->simplicity_score}}</td>
                    <td>{{$result->age_score}}</td>
                    <td>{{$result->borrowed_score}}</td>
                    <td>{{$result->description}}</td>
                    <td>{{$result->typical_context}}</td>
                    <td>{{$result->representation}}</td>
                    <td>{{$result->sub_code}}</td>
                </tr>
            @endforeach
            </tbody>
        </table>

== 编辑#2 ==

刚在这里做了一些其他测试,发现使用

  $results = DB::table('lemma')
  ->join('etymology', 'lemma.id', '=', 'etymology.lemma_id_fk')
  ->join('gloss', 'etymology.id', '=', 'gloss.etymology_id')
  ->join('wold_meanings', 'gloss.gloss', '=', 'wold_meanings.meaning')
  ->select(DB::raw('lemma.*'))
  ->where('gloss.gloss', 'like', '%flower%')
  ->get();

它只给我 table 引理的结果,即它忽略了连接。所以我在 <td>{{$result->id}}</td><td>{{$result->pageid}}</td> 的 table 视图中收到结果,但没有收到其他结果。

从链中删除对 select() 的调用将使它起作用。

select 方法表示查询的 SELECT 部分。这样,写下:

->select(DB::raw('lemma.*'))

等于:

SELECT lemma.* FROM ...

换句话说,您只是 selecting 来自 lemma table 的列。

但是,由于您还想 select 来自其他 table 的列,省略链的那一部分将使查询生成器回退到其默认行为,这与:

SELECT * FROM ...

如果您想明确说明自己在 select 的内容,这样做会达到同样的效果:

->select('*')