So I am trying a few complex ordering filters in Eloquent. So I have a patent_text table. Here is the migration table for it:
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');
});
So now I am trying to order by text_type
and I want to have ORIGINAL
first and then MACHINE
but that is easy.
I just do $this->texts->orderBy('text_type');
and it works just fine. The ORIGINAL
texts are put first and then the other ones.
But I also want to order by the language field. I want en
to be on top and then others. So I tried orderByRaw
like so:
$this->texts()->orderBy('text_type')->orderByRaw("FIELD(language , 'en', 'se') ASC")->get()
But it did not work and I cannot figure out what I am missing.
您可以与CASE
一起使用orderByRaw
而不是FIELD
例如
->orderByRaw("CASE WHEN language = 'en' THEN 0 WHEN language = 'se' THEN 0 ELSE 1 END")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.