Arrays 将数组正确用于laravel和数据库调用
大家好,我的情况是这样的,我正在将stripe集成到我的网站中,该网站建立在laravel 5.2框架上,当我们向某人发出账单时,我们将首先选择这是否是一个再次发生的账单,如果是,那么我将使用我们目前在stripe中创建的计划id显示一个下拉列表,其中包含所有计划,一旦选择了一个计划,我希望它能自动在表格中填写计划的名称和价格。我目前正在我的控制器中调用以检索条带帐户中的所有计划,然后我们遍历集合或数组并获取计划的id,然后将计划id传递到视图中的下拉菜单。但是,当选择某个计划时,我需要能够获取该计划的其他信息,以便我可以自动填写表单,我如何才能做到这一点 我已经准备好javascript来自动填充表单,我只是不知道如何从所选计划中获取我需要的其他值,例如名称和价格。我感觉我没有正确使用数组。感谢您的帮助 行政控制员Arrays 将数组正确用于laravel和数据库调用,arrays,laravel,Arrays,Laravel,大家好,我的情况是这样的,我正在将stripe集成到我的网站中,该网站建立在laravel 5.2框架上,当我们向某人发出账单时,我们将首先选择这是否是一个再次发生的账单,如果是,那么我将使用我们目前在stripe中创建的计划id显示一个下拉列表,其中包含所有计划,一旦选择了一个计划,我希望它能自动在表格中填写计划的名称和价格。我目前正在我的控制器中调用以检索条带帐户中的所有计划,然后我们遍历集合或数组并获取计划的id,然后将计划id传递到视图中的下拉菜单。但是,当选择某个计划时,我需要能够获取
try{
$stripePlanIdsServer = Plan::all()->data;
$stripePlanIds = array();
if($stripePlanIdsServer){
foreach($stripePlanIdsServer as $stripePlanId){
$stripePlanIds[] = $stripePlanId->id;
}
}
}catch (\Exception $e){
return redirect()->route('users.administration.index')->with('error', $e->getMessage());
}
return view('users.administration.index', compact('stripePlanIdsServer', 'stripePlanIds'));
行政观点
<div class="row">
<div class="col-sm-4 text-center">
<div class="form-group label-floating">
{!! Form::label('reoccurring', 'Reoccurring') !!}
</br>
{!! Form::select('reoccurring', ['Yes'=>'Yes','No'=>'No'], null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'reoccurring'])!!}
</div>
</div>
<div class="col-sm-4 text-center">
<div id="planIdDiv" class="form-group label-floating" style="display: none;">
{!! Form::label('planId', 'Plan ID (shows up on customers bank statement') !!}
</br>
{!! Form::select('planId', $stripePlanIds, null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'plan'])!!}
</div>
</div>
<div class="col-sm-2 text-center">
<div id="billStatusDiv" class="form-group label-floating" style="display: none;">
{!! Form::label('billStatus', 'Bill Status') !!}
</br>
{!! Form::select('billStatus', ['Not Paid'=>'Not Paid','Paid'=>'Paid'], null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'billStatus'])!!}
</div>
</div>
<div class="col-sm-2 text-center">
<div id="organizationDiv" class="form-group label-floating" style="display: none;">
{!! Form::label('organization', 'Assign Bill To') !!}
</br>
{!! Form::select('organization', $organization, null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option'])!!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div id="priceDiv" class="form-group label-floating" style="display: none;">
<label class="control-label">Amount to Bill (in dollars)</label>
<input id="price" type="text" name="price" class="form-control">
</div>
</div>
<div class="col-md-4">
<div id="productNameDiv" class="form-group label-floating" style="display: none;">
<label class="control-label">Product</label>
<input id="productName" type="text" name="productName" class="form-control">
</div>
</div>
</div>
好的是你已经把所有的计划传递给了你的视图。现在转到javascript并执行以下操作
var plans = <php echo json_encode($stripePlanIdsServer);>;
在Javascript中,定义showDiv函数
function showDiv(elem){
plans.map( function(item) {
if ( item.id == elem.value ) {
//.value is the selected option value
//now do something to display the hidden div
document.getElementById('hidden_plan' + item.id).style.display = "block";
}
});
}
你隐藏的计划可能是这样的
<div id="hidden_plan1" style="display: none;">Plan 1</div>
<div id="hidden_plan2" style="display: none;">Plan 2</div>
<div id="hidden_plan3" style="display: none;">Plan 3</div>
这不是解决办法,但你已经有了主意。最重要的是将plans数组从php转换为Javascript,以便在Javascript中使用它
祝你好运 非常感谢你!就像你提到的,我不得不做一些轻微的修改,但你给了我解决问题所需的知识,现在我可以再次前进了!谢谢你抽出时间!
<div id="hidden_plan1" style="display: none;">Plan 1</div>
<div id="hidden_plan2" style="display: none;">Plan 2</div>
<div id="hidden_plan3" style="display: none;">Plan 3</div>