Home > front end >  Laravel Eloquent return conditionally when first condition not exists return another
Laravel Eloquent return conditionally when first condition not exists return another

Time:10-22

i use Eloquent for get Multilanguage post title.

    public function getTitle_en()
{
    return $this->hasOne(Meta::class, 'post_id')->where(['cat' => 'title', 'meta_name' => 'en']);
}

this return english title, but some times the english title not exist so i want to return title in another language .

i mean when use getTitle_en to get engish title (['cat' => 'title', 'meta_name' => 'en']) , if it's not exist return title in another language like "nl" and again if "nl" (['cat' => 'title', 'meta_name' => 'nl']) does not exist return in another language like "gr".

so i want Eloquent check if english title exists in my table (meta) then return it, otherwise if english title not exist , check "nl" title if it exist return otherwise check next language

CodePudding user response:

You could use this solution:

$query->orderByRaw("FIELD(status, '2', '1', '3')")

by just changing your where() method into accepting function. For more details, see here.

  • Related