Reputation: 183
I am trying to pass an object from my index page to a function and I am trying to differntiate between servers + domains, I can do this on the index page by getting the class name however the variable passed to my function is only the id so I can not see if the object is a server or domain,
if i try and do this it sends the whole object as variable ($invoice):
{!! Form::open(array('class' => 'form-inline', 'method' => 'POST', 'action' => array('InvoiceController@bill', domains.'/'.$invoice))) !!}
controller
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'));
}
form
<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>
controller
public function bill($invoice) {
$invoice = Domain::whereId($invoice)->first();
$bill = new BilltoKashflow();
$bill->bill($invoice);
}
Upvotes: 0
Views: 2374
Reputation: 3421
You can pass data to your controllers via route parameter
, GET
or POST
.
If you decide to use the route parameters
, you can automaticly bind a model. See route model binding
Edit:
If your Form-Open
tag looks like this:
{!! Form::open(array('class' => 'form-inline', 'method' => 'POST', 'route' => array('handle-bill', domain->id, $invoice->id))) !!}
The corresponding route should look like this:
Route::post('/bill/{domain}/{invoice}', [
'as' => 'handle-bill',
'uses' => 'InvoiceController@bill'
]);
How the model binding has to look depends on your laravel version
Upvotes: 1