0
我有以下複雜的查詢,並希望在Laravel中使用它。但直到現在我還沒有成功。也許你們中的一個人可以展示如何將它「翻譯」到Laravel的查詢構建器。如何將複雜查詢「翻譯」爲Laravel Query Builder?
這裏,在工作臺行之有效原始查詢:
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();
它給出m e僅僅來自表引理的結果,即它忽略了聯結。因此,我從<td>{{$result->id}}</td>
的視圖表中收到結果,直到<td>{{$result->pageid}}</td>
,但不是其餘。
任何錯誤或者你得到不正確的結果? –
最後一個肯定應該工作,因爲它是相同的原始查詢。你在'$ result/$ results'變量中得到了什麼,你期望什麼? –
@MayankPandeyz既沒有錯誤也沒有不正確的結果...我認爲我只看到桌子上的那個,而結果應該出現在tbody中。而且在控制檯中也沒有。 –