0

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);
}
5
  • 1
    Please edit your question and make it more understandeable. Commented Mar 31, 2016 at 13:42
  • @GinoPane what doesnt make sense so i can change it Commented Mar 31, 2016 at 13:44
  • What do you mean with "differentiate $domain and $server"? Commented Mar 31, 2016 at 13:48
  • @barfoos I need to know if the object is a Server or Domain, which are 2 objects that I have merged into the $invoice variable Commented Mar 31, 2016 at 14:06
  • Please add the PHP tag to your question. This allows for syntax highlighting. Commented Mar 31, 2016 at 14:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

so if i wanted to bind 2 models(domain/server) to a route how would i do that?
take a look at the link in my answer ;)
change it the way i have written

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.