2015-06-14 104 views
0

我有表格,我正在使用表單模型綁定。我的形式是Laravel 5如何將2個模型傳遞給表單

{!! Form::model($vehicle,['url' => '/pages/store']) !!}         
    <table style="width:650px; margin-left: 4px;" > 
     <tbody> 
      <tr> 
       <td>ID</td> 
       <td>Model</td> 
       <td>Brand</td> 
       <td>License Plate</td> 
      </tr> 
      <tr> 
       <td>{!! Form::text('id' ,null , ['readonly'], ['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td> 
       <td>{!! Form::text('model' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td> 
       <td> 
        {!! Form::select('brand_id', $brands, null, ['id'=>'brandBox', 'style' => 'width:150px;']) !!} 
       </td> 
       <td>{!! Form::text('licenseplate' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td> 
      </tr> 
      <tr> 
       <td colspan="2">Client</td> 
      </tr> 
      <tr> 
       <td colspan="2">{!! Form::select('representive_client_id', $clients, null, ['id'=>'clientSelectBox', 'class' => 'selectbox']) !!}</td> 
      </tr> 
      <tr> 
       <td colspan="2">Telephone Number</td> 
      </tr> 
      <tr> 
       <td colspan="2">{!! Form::text('tel_number' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td> 
      </tr> 
      <tr> 
       <td colspan="2">Adress</td> 
       <td colspan="2">{!! Form::textarea('address' ,null ,['class' =>'textboxlong form-control','style'=>'height:60px;']) !!}</td> 
      </tr> 
     </tbody> 
    </table> 
    <div id="buttoncontainer"> 
     <a class="btn btn-default" href="{{ URL::to('pages/vehicleprocess/' . $first -> id) }}"><<</a>&nbsp; 
     @if($previous) 
      <a class="btn btn-default" href="{{ URL::to('pages/vehicleprocess/' . $previous) }}">PREVIOUS</a>&nbsp; 
     @endif 
     @if($next) 
     <a class="btn btn-default" href="{{ URL::to('pages/vehicleprocess/' . $next) }}">NEXT</a>&nbsp; 
     @endif 
     <a class="btn btn-default" href="{{ URL::to('pages/vehicleprocess/' . $last -> id) }}">>></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <a class="btn btn-default" id="add">EKLE</a>&nbsp; 
     {!! Form::submit('EDIT', array('class'=>'btn btn-primary')) !!}&nbsp; 
     {!! Form::submit('NEW RECORD', array('class'=>'btn btn-primary')) !!}&nbsp; 
    </div> 
{!! Form::close() !!} 

我傳遞的$vehicle作爲

$vehicle = Vehicle::where('vehicles.id',$id)->join('clients', 'vehicles.representive_client_id', '=', 'clients.id')->first(); 

存儲功能

$client = new Client; 
$client -> full_name = $client_id; 
$client -> tel_number = $tel_number; 
$client -> mobile_number = $mobile_number; 
$client -> save(); 

$last_client_id = $client -> id; 

$input = Request::except('client_id'); 
$vehicle = Vehicle::create($input); 

$u_vehicle = Vehicle::orderBy('created_at', 'desc')->first(); 
$u_vehicle -> update(array('client_id' => $last_client_id)); 

我能夠看到在我看來這些領域的所有值,但是當涉及到存儲新紀錄到我的數據庫我得到這個錯誤

列沒有找到未知列「tel_number」

想我需要通過2種型號(車輛和客戶端)的形式,但不知道如何做到這一點。任何幫助,將不勝感激。

+0

請提供您嘗試存儲車輛時的代碼。如果您嘗試更新車輛關係,則無需將兩個模型傳遞給您可以通過關係更新的視圖:http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models – haakym

+0

@ haakym我需要通過使用這種形式更新車輛和客戶端 – Tartar

+0

當然,我假設他們是相關的 - 對不對?您可以使用試圖將模型存儲在數據庫中的代碼編輯/更新您的問題,我很樂意提供幫助。這是導致錯誤的代碼。謝謝。 – haakym

回答

0

總之,你需要通過你的觀點VehicleClient的實例 - 我知道你正在試圖做到這一點,但我猜測這是不正確的做法。我使用Laravel設置了一個乾淨的安裝,當您在模型上建立關係時它似乎可以工作。

我不確定你們的關係是什麼,所以我們假設Client有一個Vehicle,所以Vehicle屬於Client。因此,我們可以把你的關係,你的Vehicle.php

public function client() { 
    return $this->belongsTo('App\Client'); // or whatever the namespace to your class is 
} 

詳見這裏定義關係:http://laravel.com/docs/5.1/eloquent-relationships#defining-relationships

然後當您加載視圖中的模型,你可以這樣做:

$vehicle = \App\Vehicle::with('client')->where('id', $id)->first(); 

的重要的部分是:with('client')

然後任何帶有Client屬性的文本字段應該是充滿現有的數據。

我希望有幫助,如果您需要更多幫助,請讓我知道。我有一個在空白的laravel項目中工作的示例代碼,所以如果你需要的話,我可以把它放在github上。

+0

這工作表示感謝!但是,如何獲得定義爲「{!!」的文本框值Form :: text('representive [tel_number]',null,['class'=>'textboxlong form-control','style'=>'height:23px;'])!!}' – Tartar

+0

不客氣,很高興你的工作。您的數據庫中有代表性的另一個實體與車輛或客戶有關嗎?如果是這樣,只需在設置關係之後加載視圖之前將( - 'representative')'追加到查詢中。 – haakym

+0

它是關係函數的名稱 – Tartar

相關問題