从laravel 10中的一对多关系中获取随机顺序的方法。

huangapple go评论59阅读模式
英文:

How to fetch random order from One-to-Many relationships in laravel 10

问题

我有一个一对多的关系

  • "Question"表有很多选项,
  • "Options"表属于"Question"表。

我尝试同时获取问题和选项的随机顺序。

我已经得到了问题的随机顺序,但我不知道如何获取选项的随机顺序

以下是我的代码:

我的控制器:

public function showQ(){
    return view('my_view',[
        
        'Questions' => Question::inRandomOrder()->with('Option')->get(),
        
    ]);
}

在视图中:

@foreach($Questions as $question)
  
   {{$question->DbQuestions}}

   @foreach($question->Option as $option)
       
      {{$option->DbOptions}}
   
   @endforeach

@endforeach

Question 模型:

public function Option(): HasMany {
    return $this->hasMany(Option::class);
}

Option 模型:

public function Question(): BelongsTo {
    return $this->belongsTo(Question::class);
}

有人能解释这个问题吗?

英文:

I have a One-to-Many relationship

  • The "Question" table has many Options,
  • The "Options" table belongtsTo "Question" table.

I’am trying to fetch, in the same time, random order for my question and random order for options.

I’ am getting the Questions in random order, but I don’t know how to get a random order for the options as well.

Here below you have my code :

My controller:

    public function showQ(){
        return view('my_view',[
            
            'Questions' => Question::inRandomOrder()->with('Option')->get(),
            
        ]);

In the view

    @foreach($Questions as $question)
      
       {{$question -> DbQuestions}}

       @foreach($Questions -> Option as $option)
           
          {{$option -> DbOptions}}
       
       @endforeach

    @endforeach
    

Question model

  public function Option(): HasMany {
        return $this -> hasMany(Option::class);
    }

Option model

  public function Question(): BelongsTo {
        return $this-> belongsTo(Question::class);
    }

Is there any one to clarify the issues?

答案1

得分: 1

Here's the translated content without the code parts:

注意:

> (最佳实践 1) 始终使用 单数/复数 并始终以 小写字母 写关系。使用 options 而不是 OptionOptions

> (最佳实践 2) 使用 有意义的函数名称,如果使用 show,那么 showQ 会更好。 (标准 资源控制器方法


在视图中,您可以将 inRandomOrder() 移入子函数,如下所示:

在 View 中:

@foreach($Questions as $question)
   {{$question->DbQuestions}}

   @foreach($question->options as $option)           
      {{$option->DbOptions}}       
   @endforeach
@endforeach

Question 模型中:

public function options(): HasMany {
    return $this->hasMany(Option::class);
}

Option 模型中:

public function question(): BelongsTo {
    return $this->belongsTo(Question::class);
}
英文:

Note:

> (Best Practise 1) singular/plural and always write relationship in a small case. options not Option or Options.

>(Best Practise 2) Use meaningful function names showQ would be nice if you use show. (Standard Resource controller methods)


You can move inRandomOrder() into sub function like this

public function showQ(){
    return view('my_view',[
        'Questions' => Question::inRandomOrder()
            ->with(['Option' => function ($query) {
                $query->inRandomOrder();
            }])
            ->get(),
    ]);
}

In View

@foreach($Questions as $question)
   {{$question->DbQuestions}}

   @foreach($question->options as $option)           
      {{$option->DbOptions}}       
   @endforeach
@endforeach

In Question Model

public function options(): HasMany {
    return $this -> hasMany(Option::class);
}

In Option Model

public function question(): BelongsTo {
    return $this-> belongsTo(Question::class);
}

huangapple
  • 本文由 发表于 2023年5月10日 18:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定