laravel使用关系在多个表中插入数据

laravel使用关系在多个表中插入数据,laravel,Laravel,用户将同时在事故、车辆表中添加数据 我设法将数据添加到事故表中,但没有添加到我的代码所在的车辆上 用户型号: class User extends Model implements Authenticatable{ use \Illuminate\Auth\Authenticatable; public function accident(){ return $this->hasMany('App\Accident'); } } cla

用户将同时在事故、车辆表中添加数据

我设法将数据添加到事故表中,但没有添加到我的代码所在的车辆上

用户型号:

class User extends Model implements Authenticatable{

     use \Illuminate\Auth\Authenticatable;

     public function accident(){
         return $this->hasMany('App\Accident');
     }
}
class Accident extends Model{

     public function user(){
         return $this->belongsTo('App\User');
     }
     public function vehicle(){
         return $this->hasMany('App\Vehicle');
     }
}
class Vehicle extends Model{

     public function accident(){
         return $this->belongsTo('App\Accident');
     }
 }
 public function postAccident(Request $request){

    $accidentLocation = $request['location'];
    $accidentDateTime = Carbon::now('Asia/Manila')->format('y-m-d g:i:s A');

    $accident = new Accident();
    $accident->accidentLocation = $accidentLocation;
    $accident->accidentDateTime = $accidentDateTime;

    $request->user()->accident()->save($accident);

    $vehicle = new Vehicle(); // <-- Im trying to do this
    $vehicle->vehicleType = "SUV";

    $vehicle()->save();

    return response()->json(['msg' => "Successfully submitted"], 200);  

}
事故模式:

class User extends Model implements Authenticatable{

     use \Illuminate\Auth\Authenticatable;

     public function accident(){
         return $this->hasMany('App\Accident');
     }
}
class Accident extends Model{

     public function user(){
         return $this->belongsTo('App\User');
     }
     public function vehicle(){
         return $this->hasMany('App\Vehicle');
     }
}
class Vehicle extends Model{

     public function accident(){
         return $this->belongsTo('App\Accident');
     }
 }
 public function postAccident(Request $request){

    $accidentLocation = $request['location'];
    $accidentDateTime = Carbon::now('Asia/Manila')->format('y-m-d g:i:s A');

    $accident = new Accident();
    $accident->accidentLocation = $accidentLocation;
    $accident->accidentDateTime = $accidentDateTime;

    $request->user()->accident()->save($accident);

    $vehicle = new Vehicle(); // <-- Im trying to do this
    $vehicle->vehicleType = "SUV";

    $vehicle()->save();

    return response()->json(['msg' => "Successfully submitted"], 200);  

}
车型:

class User extends Model implements Authenticatable{

     use \Illuminate\Auth\Authenticatable;

     public function accident(){
         return $this->hasMany('App\Accident');
     }
}
class Accident extends Model{

     public function user(){
         return $this->belongsTo('App\User');
     }
     public function vehicle(){
         return $this->hasMany('App\Vehicle');
     }
}
class Vehicle extends Model{

     public function accident(){
         return $this->belongsTo('App\Accident');
     }
 }
 public function postAccident(Request $request){

    $accidentLocation = $request['location'];
    $accidentDateTime = Carbon::now('Asia/Manila')->format('y-m-d g:i:s A');

    $accident = new Accident();
    $accident->accidentLocation = $accidentLocation;
    $accident->accidentDateTime = $accidentDateTime;

    $request->user()->accident()->save($accident);

    $vehicle = new Vehicle(); // <-- Im trying to do this
    $vehicle->vehicleType = "SUV";

    $vehicle()->save();

    return response()->json(['msg' => "Successfully submitted"], 200);  

}
我的控制器和数据:

class User extends Model implements Authenticatable{

     use \Illuminate\Auth\Authenticatable;

     public function accident(){
         return $this->hasMany('App\Accident');
     }
}
class Accident extends Model{

     public function user(){
         return $this->belongsTo('App\User');
     }
     public function vehicle(){
         return $this->hasMany('App\Vehicle');
     }
}
class Vehicle extends Model{

     public function accident(){
         return $this->belongsTo('App\Accident');
     }
 }
 public function postAccident(Request $request){

    $accidentLocation = $request['location'];
    $accidentDateTime = Carbon::now('Asia/Manila')->format('y-m-d g:i:s A');

    $accident = new Accident();
    $accident->accidentLocation = $accidentLocation;
    $accident->accidentDateTime = $accidentDateTime;

    $request->user()->accident()->save($accident);

    $vehicle = new Vehicle(); // <-- Im trying to do this
    $vehicle->vehicleType = "SUV";

    $vehicle()->save();

    return response()->json(['msg' => "Successfully submitted"], 200);  

}
车辆:

public function up()
{
    Schema::create('accidents', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('accidentLocation', 100);
        $table->dateTime('accidentDatetime');
        $table->string('weatherCondition', 30);
        $table->string('accidentCase', 20);
        $table->string('roadCharacter', 20);
        $table->string('roadCondition', 20);
        $table->string('collisionType', 20);
        $table->string('accidentType', 20);
        $table->integer('vehicleNumber');
        $table->string('accidentStatus', 10);
        $table->text('file_attachment');
        $table->string('user_id');
    });
}
 public function up()
{
    Schema::create('vehicles', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->integer('accident_id')->unsigned();
        $table->foreign('accident_id')->references('id')->on('accidents');
        $table->string('vehicleType', 20);
        $table->string('vehicleMake', 20);
        $table->string('vehicleModel', 20);
        $table->string('vehiclePlateNumber', 10);
        $table->string('vehicleClassification', 15);
        $table->string('vehicleOwner', 50);
        $table->string('vehicleRegistration', 30);

        $table->integer('driver_id')->unsigned();
        $table->foreign('driver_id')->references('id')->on('drivers');
    });
}

使用
elount
关系保存数据

public function postAccident(Request $request){

    $accidentLocation = $request['location'];
    $accidentDateTime = Carbon::now('Asia/Manila')->format('y-m-d g:i:s A');

    $accident = new Accident();
    $accident->accidentLocation = $accidentLocation;
    $accident->accidentDateTime = $accidentDateTime;

    $request->user()->accident()->save($accident);

    $vehicle = new Vehicle(); // <-- Im trying to do this
    $vehicle->vehicleType = "SUV";

    // Save the vehicle associates accident
    $accident->vehicle()->save($vehicle);



    return response()->json(['msg' => "Successfully submitted"], 200);  

}
$incident->vehicle()->save($vehicle);这是正确的

我删除了$table->integer('driver_id')->unsigned();
$table->foreign('driver_id')->references('id')->on('drivers');它能工作

错误是什么?而不是
$vehicle()->save()
TRY
$incident->vehicle()->>attach($vehicle->id)
badmethod attach error我以前尝试过这个方法,但错误是SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
pards
vehicles
,CONSTRAINT
vehicles\u driver\u id\u foreign
外键(
driver\u id
)引用
drivers
id
)(SQL:insert-in
vehicleletype
事故id
更新,
创建)值(SUV,182016-05-2604:26:452016-05-2604:26:45)@MajikeroGallardo您可以为此包含迁移文件吗?迁移文件postedRemove驱动程序id外键现在我知道这是一个旧答案,但此代码包含两个调用
save
。Eloquent,ORM,应该确定并执行此操作。这是负责将内存中的对象与其存储的co同步的组件这是我的副本