【问题标题】:How to check if the user possible choices contain results如何检查用户可能的选择是否包含结果
【发布时间】:2021-09-17 08:22:43
【问题描述】:

我有这个带有步骤的表单,在每个步骤中表单都通过 AJAX 提交和返回结果。

当用户选择第一步时,第二步将被激活,并向用户显示三个选项,这些选项可能包含也可能不包含进一步的结果,具体取决于用户的选择。问题是我想根据选择是否包含结果来限制用户在第二步中可能的选择。

基本上我需要事先运行查询,如果它们包含结果,那么它们应该出现,否则它们不应该是可见的。


$this->firstChoice = (int) \Input::get('first_choice');
$this->secondChoice = (int) \Input::get('second_choice');

$this->results = Class::with('relation');

if(isset($this->firstChoice ) && $this->firstChoice > 0) {
    $id = $this->firstChoice ;
    $this->results->whereHas('first-relationship', function($q) use($id) {
        $q->where('id', $id);
    })
}

if(isset($this->secondChoice ) && $this->secondChoice > 0) {
    $id = $this->secondChoice ;
    $this->results->whereHas('second-relationship', function($q) use($id) {
        $q->where('id', $id);
    })
}

** before the second step is checked i need to run a query check on the second 
relationship and if the possible choice contains results then i need to show only those options **

我尝试过类似的方法,但没有成功。


if($this->results->whereHas('second-relationship', function($q) {
     $q->where('id', 1);
})->count() > 0) {
    $newArray = ['push results here and then return it to the front end']
} // this repeated 3 times for the three possible choices

** ID = 1 for the first option id - i have tried it hard coded and also in a foreach loop
where the id's were dynamic but without any success **

第二次我运行查询以检查选择是否有可能导致代码中断。

我只想知道如何对用户可能的选择及其结果进行检查。

我应该如何解决这个问题,非常感谢任何帮助。

在 10 月的 CMS 中编码。

【问题讨论】:

  • 我已经添加了答案,但似乎没有您的数据库架构,很难预测完整的解决方案

标签: laravel octobercms


【解决方案1】:

您可以这样做,您需要根据当前的选择进行下一步。

例如:第一步是CHOICES,它确实有下一个选择(我在下面解释)。

现在,当用户选择并提交结果时,制作 ajax 并呈现另一个带有 CHOICES 的表单,这确实有下一个选择。

// for OUR form NO. 1
// Retrieve all choose which has at lesat one choice
// this could be your `onShowFirstForm`
$choices = Choice::has('children')->get();

// NOW user select one of them
// for Next form prepare choices
// this could be your `onNextForm`
$choices = Choice::find($selectedChoiseId)->has('children')->fetch();

// and so on
// you can continue ... with this

这里只有一站,在某些时候你会得到$choices 将是空白的,因为他们不能有更多的child -> 那时你可以只显示消息等

如有任何疑问,请发表评论。

【讨论】:

  • 您好 Hardik,感谢您的回复,为了更清楚,我有一个模型,其中包含我想根据第一步选择限制的第二步中的所有 cohoices,我将模型结果显示在onRender 函数 ex; $this['results'] = Model::all(),我需要修改该变量并将其结果传递给我在提交时刷新的部分。我需要知道一种方法,如何根据用户在第一步中选择的内容,在第二步中使用所有可能的选择对 $this->results 集合运行查询。我希望我足够清楚我想要共犯的东西。谢谢。
  • 好的,这个问题是关于数据库/建模或部分渲染方面的
  • 问题是我找不到一种方法来运行检查查询来检查 $this->results 集合是否有基于第二步中的三个可能选择的结果,呈现在我得到想要的结果之后,我可以毫无问题地实现结果。在我看来,我需要检查该集合是否有结果,然后是否填充了一个数组,该数组稍后会作为部分刷新返回。
  • 提出我的问题的最简单方法如下:我想检查一个集合关系是否有任何基于 id(id = 1、2 和 3)的结果,以及该集合是否有结果然后是一个新的集合并将结果推送到那里。选择第一步时,第二步有选项 1、2 和 3,选项 1 选择时没有项目,选项 2 中殿 3 有。把这些选择放在一个集合中,稍后我会展示给前端。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
相关资源
最近更新 更多