Php Laravel 5如何将2个模型传递到表单
我有表单,我正在使用表单模型绑定。我的表格是Php Laravel 5如何将2个模型传递到表单,php,forms,laravel,model,laravel-5,Php,Forms,Laravel,Model,Laravel 5,我有表单,我正在使用表单模型绑定。我的表格是 {!! Form::model($vehicle,['url' => '/pages/store']) !!} <table style="width:650px; margin-left: 4px;" > <tbody> <tr> <td>ID&l
{!! 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() !!}
存储功能
$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));
我可以在我的视图中看到这些字段的所有值,但是当涉及到将新记录存储到数据库时,我遇到了这个错误
未找到列未知列“电话号码”
我想我需要将2个模型(车辆和客户机)传递给表单,但不确定如何进行传递。任何帮助都将不胜感激。简而言之,您需要通过
客户机向视图传递车辆的一个实例-我知道您正试图通过加入来实现这一点,但我猜这不是正确的方式。我与Laravel建立了一个干净的安装,当您在模型上建立关系时,它似乎可以工作
我不确定您的关系是什么,因此让我们假设客户机
有一辆车辆
,因此车辆
属于客户机
。因此,我们可以将您的关系放入您的Vehicle.php
public function client() {
return $this->belongsTo('App\Client'); // or whatever the namespace to your class is
}
有关定义关系的更多信息,请参见此处:
然后,在视图中加载模型时,可以执行以下操作:
$vehicle = \App\Vehicle::with('client')->where('id', $id)->first();
重要的部分是:with('client')
然后,应使用现有数据填充具有Client
属性的任何文本字段
我希望这有帮助,如果你需要更多的帮助,请告诉我。我有一个空白的laravel项目中的示例代码,因此如果您需要查看,我可以在github上发布。请在您尝试存储车辆时提供代码。如果您试图更新与车辆的关系,则不需要将两个模型传递到视图,您可以通过关系更新:@haakym我需要使用此formSure更新车辆和客户端,我假设它们是相关的-对吗?你能用代码编辑/更新你的问题吗?我很乐意为你提供帮助。这是导致错误的代码。谢谢。@haakym我已经更新了这个问题,请看一看,你是如何确定它是新记录还是更新的,除非你使用javascript,否则你的更新和新记录按钮将提交到完全相同的位置?谢谢!但是如何获得定义为{!!Form::text('representive[tel_number]',null,['class'=>'textboxlong Form control','style'=>'height:23px;'])的文本框值!!}
不客气,很高兴它能正常工作。您的DB中是否有与车辆或客户相关的另一个实体?如果是这样,在设置关系后加载视图之前,只需在查询中附加->和('representative')
。这是车辆中关系函数的名称,对吗?与客户机或其他事物的关系是什么?
$vehicle = \App\Vehicle::with('client')->where('id', $id)->first();