我有表单,我正在使用表单模型绑定。我的表格是
{!! 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>
@if($previous)
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $previous ) }}">PREVIOUS</a>
@endif
@if($next)
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $next ) }}">NEXT</a>
@endif
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $last -> id ) }}">>></a>
<a class="btn btn-default" id="add">EKLE</a>
{!! Form::submit('EDIT', array('class'=>'btn btn-primary')) !!}
{!! Form::submit('NEW RECORD', array('class'=>'btn btn-primary')) !!}
</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个模型(车辆和客户端)传递给表单,但不确定如何进行。任何帮助,将不胜感激。
简而言之,您需要传递一个Vehicle
with的实例的视图Client
-我知道您正在尝试通过join进行操作,但是我猜这不是正确的方法。我使用Laravel进行了全新安装,当您在模型上建立关系时,它似乎可以正常工作。
我不确定您的关系是什么,因此让我们假设aClient
有一个关系,Vehicle
那么theVehicle
属于the 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上。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句