我正在尝试将一个对象从我的索引页面传递给一个函数,我试图在服务器+域之间进行区分,我可以通过获取类名在索引页面上执行此操作但是传递给我的函数的变量只是id,所以我无法看到对象是服务器还是域,
如果我尝试这样做,它会将整个对象作为变量发送($ invoice):
{!! Form::open(array('class' => 'form-inline', 'method' => 'POST', 'action' => array('InvoiceController@bill', domains.'/'.$invoice))) !!}
控制器
public function index() {
$expiry = date('Y-m-d', strtotime('+3 months'));
$servers = Server::where('expiry_date', '<', $expiry)->orderBy('expiry_date', 'asc')->get();
$domains = Domain::where('expiry_date', '<', $expiry)->orderBy('expiry_date', 'asc')->get();
$invoices = $domains->merge($servers);
return view('invoices.index', compact('servers'))->with(compact('domains'))->with(compact('invoices'));
}
形式
<tbody>
@foreach($invoices as $invoice)
<?php $reflect = new \ReflectionClass($invoice); ?>
{!! Form::open(array('class' => 'form-inline', 'method' => 'POST', 'action' => array('InvoiceController@bill', $invoice))) !!}
<tr>
@if ($reflect->getShortName() == 'Domain')
<td><a href="{{ route('domains.show', $invoice->id) }}">{{ $invoice->id }}</a></td>
@endif
@if ($reflect->getShortName() == 'Server')
<td><a href="{{ route('servers.show', $invoice->id) }}">{{ $invoice->id }}</a></td>
@endif
<td>{{ $invoice->name }}</td>
<td>{{ $invoice->expiry_date }}</td>
<td>{{ $reflect->getShortName() }}</td>
<td>{{ $invoice->ClientName }}</td>
<td>
@if ($reflect->getShortName() == 'Domain')
{!! link_to_route('domains.edit', 'Edit', array($invoice->id), array('class' => 'btn btn-info')) !!}
@endif
@if ($reflect->getShortName() == 'Server')
{!! link_to_route('servers.edit', 'Edit', array($invoice->id), array('class' => 'btn btn-info')) !!}
@endif
{!! Form::submit('Bill', array('class' => 'btn btn-danger')) !!} </td>
</tr>
{!! Form::close() !!}
@endforeach
</tbody>
控制器
public function bill($invoice) {
$invoice = Domain::whereId($invoice)->first();
$bill = new BilltoKashflow();
$bill->bill($invoice);
}
答案 0 :(得分:1)
您可以通过route parameter
,GET
或POST
将数据传递给您的控制器。
如果您决定使用route parameters
,则可以自动绑定模型。见route model binding
<强> 编辑: 强>
如果您的Form-Open
标记如下所示:
{!! Form::open(array('class' => 'form-inline', 'method' => 'POST', 'route' => array('handle-bill', domain->id, $invoice->id))) !!}
相应的路线应如下所示:
Route::post('/bill/{domain}/{invoice}', [
'as' => 'handle-bill',
'uses' => 'InvoiceController@bill'
]);
模型绑定的外观取决于您的laravel版本