【问题标题】:How to show the custom fields of each ticket type?如何显示每种工单类型的自定义字段?
【发布时间】:2018-04-10 16:27:39
【问题描述】:

我有一个 single.blade.php 文件,其中显示了大会详细信息,还有一个表格,以便用户可以为每种票证类型选择他想要的票证数量。

当用户选择数量并单击下一步时,请求转到具有 storeQuantity() 方法的 RegistrationController 来存储所选数量 由用户在 $selectedTypes 数组中,并使用 $selectedTypes 数组将用户重定向到registration.blade.php:

class RegistrationController extends Controller
{
     public function storeQuantityandRedirect(Request $request, $id){

            $typeQuantities = $request->get('types');

            foreach($typeQuantities as $typeName => $quantity){
                $type = TicketType::where('name', $typeName)->firstOrFail();
                $price = $type->price;
                $selectedTypes[$type->name]['quantity'] = $quantity;
                $selectedTypes[$type->name]['price'] = $price;

            }
            return view('congresses.registration')->with('selectedTypes', $selectedTypes);
        }
}

现在在registration.blade.php文件中,用户需要填写注册表。

如果用户选择了,例如注册页面上一页的2张票,应该出现两个部分供用户介绍 每个票类型的姓名和电子邮件。这适用于以下代码:

@foreach($selectedRtypes as $k=>$selectedRtype)
    <h6>Participant - 1 - Ticket Type - {{$k}}</h6> <!-- The variable {{$k}} shows the name of each ticket type (ex: full access, basic, etc). -->
    <div class="form-group font-size-sm">
        <label for="name" class="text-gray">Name</label>
        <input type="text" class="form-control" id="name" value="">
    </div>
    <div class="form-group font-size-sm">
        <label for="surname" class="text-gray">Surname</label>
        <input type="text" class="form-control" name="surname" id="surname" value="">
    </div>
@endforeach

怀疑:

每种工单类型都可以有自定义问题。

我的疑问是如何显示除了姓名和姓氏字段外,还显示每种特定票证类型的自定义字段。 例如,会议组织者可能已经为工单类型“完全访问”创建了自定义问题“您的电子邮件是什么?”。所以在 foreach 当票证类型为“完全访问”时,除了姓名和姓氏外,还应显示“您的电子邮件是什么?”字段。

你知道怎么做吗?

我有这个表格内容和关系:

questions table:
id      question                congress_id
1       whats your email             1

ticket_type_questions table:
id        ticket_type_id      question_id
1            1                     1      (the ticket type 1 has the question 1)

ticket types table
id             name        congress_id
1             full access        1
2             basic               1

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    由于您将问题存储在数据库表中,因此您可以将它们从控制器传递到您的视图。在您的控制器中,您可以添加如下内容:

    $customQuestions = Question::where('congress_id', $cong_id); 
    

    然后修改你的回报看起来像这样。

    return response()->view('congress.registration', ['selectedTypes' => $selectedTypes, 'customQuestions' => $customQuestions], 200);
    

    然后,在您看来,就在@endforeach 之前,您可以添加一个条件来检查是否有任何自定义问题。

    // put this right before your @endforeach
    @if(count($custQuestions) > 0)
    // loop and display custom question fields.
    @endif
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢。但它不起作用自定义问题没有出现。
    • 你能告诉我你用来显示自定义问题的代码吗?目前,此错误最可能的两个来源是查询无法正常工作或视图中的代码无法正常工作。
    • if (!empty($customQuestions)) if(count($customQuestions) > 0) foreach($customQuestions as $customQuestion)
      endforeach endif endif
    • 你是用刀片写的吧?将“如果”替换为“@if”。与“foreach”相同。这是我认为观点有问题的唯一地方。如果仍然无法正常工作,我会检查您的控制器中的查询是否正常工作。
    • 谢谢,是的,我正在使用@。似乎正在运行的查询“ dd($customQuestions); ”显示:
    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多