mardi 30 octobre 2018

Laravel: Get relationship in randomized order with condition

I have the following code where I get a random question with it's answers:

$q = Question:with(['answers' => function ($q) {
    $q->inRandomOrder();
}])->inRandomOrder()->first();

but i want like this:

$q = Question:with(['answers' => function ($q) {
    if(Question->random_answer==true){
        $q->inRandomOrder();
    }else{
        $q->orderBy('sort',' ASC');    
    }
}])->inRandomOrder()->first();

Home some one can solve this.

Thanks,

Best regard.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire