【发布时间】: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