2016-03-31 40 views
0

我想從我的索引頁面傳遞一個對象到一個函數,我試圖在服務器+域之間進行區分,我可以通過獲取類名稱在索引頁上執行此操作,但變量傳遞給我的功能僅僅是ID,所以我不能看到,如果對象是一個服務器或域,從刀片到控制器laravel變量

如果我試圖做到這一點將整個對象作爲變量($發票):

 {!! Form::open(array('class' => 'form-inline', 'method' => 'POST', 'action' => array('[email protected]', 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('[email protected]', $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); 
} 
+1

請編輯您的問題,使其更難以理解。 –

+0

@GinoPane什麼沒有意義,所以我可以改變它 – dev7

+0

你是什麼意思與「區分$域」和'$服務器「? – barfoos

回答

1

您可以通過route parameterGETPOST數據傳遞到控制器。如果您決定使用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' => '[email protected]' 
]); 

如何模型綁定必須看你的l aravel版本

+0

所以如果我想綁定2個模型(域/服務器)到一條路線,我會怎麼做? – dev7

+0

看看我的答案的鏈接;) –

+0

改變它我寫的方式 –