[英]How to use Eloquent ORM's getQueryLog() outside of Laravel?
[英]How to use orderByRaw correctly in laravel's eloquent ORM?
所以我正在Eloquent中嘗試一些復雜的排序過濾器。 所以我有一個patent_text表。 這是它的遷移表:
Schema::create('patent_text', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('patent_id')->unsigned()->index('patent_id');
$table->enum('text_type', array('ICLAIM', 'CLAIM', 'DESCRIPTION', 'ABSTRACT', 'TITLE'))->nullable();
$table->enum('type', array('ORIGINAL', 'MACHINE'))->nullable();
$table->string('language', 2)->nullable();
$table->longText('text')->nullable();
$table->timestamps();
$table->foreign('patent_id',
'patent_text_ibfk_1')->references('id')->on('patent')->onUpdate('RESTRICT')->onDelete('CASCADE');
});
因此,現在我嘗試按text_type
進行訂購,我想先擁有ORIGINAL
,然后再獲得MACHINE
但這很容易。
我只是做$this->texts->orderBy('text_type');
而且效果很好。 ORIGINAL
文本放在首位,然后放置其他文本。
但我也想按語言字段排序。 我想讓en
成為第一,然后是其他。 所以我像這樣嘗試了orderByRaw
:
$this->texts()->orderBy('text_type')->orderByRaw("FIELD(language , 'en', 'se') ASC")->get()
但這沒有用,我無法弄清我所缺少的。
您可以與CASE
一起使用orderByRaw
而不是FIELD
例如
->orderByRaw("CASE WHEN language = 'en' THEN 0 WHEN language = 'se' THEN 0 ELSE 1 END")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.