Laravel 5.4:使用eloquent和fillable字段更新多个表

时间:2018-03-01 08:20:11

标签: laravel eloquent laravel-5.4

我有两个模型DriverDriverCar

我需要按模型显示字段值,让我向您展示我的表单标题代码 请注意DriverDriverCar

相关driver_id
{!! Form::model($driver, [
            'route'=>['drivers.update', $driver],
            'method'=>'PATCH',
            'class'=>'form-horizontal'
            ]) !!}

现在我的表单字段中有

<div class="form-group">
     {!! Form::label('name', trans('interface.DriverName'), ['class'=>'col-sm-2 control-label']) !!}
     <div class="col-sm-10">
          {!! Form::text('name', null, ['class'=>'form-control', 'placeholder'=>trans('interface.DriverName')]) !!}
     </div>
</div>

在我更新时成功更新。但DriverCar字段如果设置为fillable,则null字段未获取$driver->driverCar->car_model字段我必须通过<div class="form-group"> {!! Form::label('car_model', trans('interface.carModel'), ['class'=>'col-sm-2 control-label']) !!} <div class="col-sm-10"> {!! Form::text('car_model', $driver->driverCar->car_model, ['class'=>'form-control', 'placeholder'=>trans('interface.carModel')]) !!} </div> </div> 等关系获取fillable

$driver->driverCar->car_model

有没有办法让它public function update( Request $request, Driver $driver, DriverCar $driverCar ) { //dd( $driverCar ); $input = $request->all(); $driver->fill( $input )->save(); $driverCar->fill( $input )->save(); return redirect()->route( 'drivers.edit', $driver ); } 而不给它Driver这样的关系?

第二 这是我的控制器我尝试一次更新两个表

DriverCar

在控制器中我也无法更新两个表,只更新 1 / \ 2 3 / \ 4 5 > tree <- list(1, list(2), list(3, list(4), list(5))) > data.tree::FromListSimple(tree, nodeName = "1") levelName 1 1 2 ¦--1 3 °--2 4 ¦--1 5 °--2 但不触及tree[[1]]

任何指南,请

1 个答案:

答案 0 :(得分:1)

您未将update ID传递给public function update( Request $request, Driver $driver) { $input = $request->all(); $driver->fill($input)->save(); $driver->driverCar()->first()->fill($input)->save(); return redirect()->route('drivers.edit', $driver); } 控制器方法,因此请将代码更改为以下内容:

* {
  box-sizing: border-box;
}

body {
  font: 15px/1.5 Arial, Helvetica, sans-serif;
  padding: 0;
  margin: 0;
  background-color: #fff;
}

/* Global */
.container {
  width: 100%;
  margin: auto;
  overflow: hidden;
  border: 1px solid red;
  text-align: center;
}

button {
  background-color: #ff5f49;
  border: none;
  padding: 15px;
  color: #fff;
  font-size: 1.2em;
}

/* Navigation */

header {
  background-color: #353c42;
  min-height: 75px;
  text-align: center;
  font-size: 1.3em;
}

nav ul li {
  display: inline;
  padding: 15px;
  text-align: center;
}
nav ul li a {
  text-decoration: none;
  text-transform: uppercase;
  color: #fff
}

/* Jumbotron */

#jumbotron {

}

#jumbotron img {
  display: block;
  width: 60%;
  margin: auto;
  margin-top: 80px;
}

.button-1 {
  width: 20%;
  display: inline-block;
  margin: 0 auto;
}

/* Clearfix */

.clearfix::after {
    content: "";
    clear: both;
    display: table;
}