我正试图将一个对象从我的索引页传递给一个函数,并试图在服务器+域之间进行区分,我可以在索引页上通过获取类名来实现这一点,但传递给我的函数的变量只是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);
}
您可以通过route parameter
、GET
或POST
将数据传递给控制器。
如果您决定使用route parameters
,则可以自动绑定模型。参见绑定的路由模型
编辑:
如果你的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版本