laravel在具有关系的多个表中插入数据

时间:2016-05-26 04:03:40

标签: laravel

用户将同时添加事故,车辆表中的数据

我设法在事故表中添加数据,但不在车辆上添加我的代码。

用户模型:

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');
    });
}

2 个答案:

答案 0 :(得分:0)

使用eloquent关系保存数据

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);  

}

你需要做的另一件事,

如果您还没有Driver模型且未保存任何

,请在迁移时将其删除
$table->integer('driver_id')->unsigned(); 
$table->foreign('driver_id')->references('id')->on('drivers');

答案 1 :(得分:-1)

$ accident-&GT;车辆() - &GT;保存($车辆);是对的。

我删除了$ table-&gt;整数('driver_id') - &gt; unsigned();         $表 - &GT;外国( 'driver_id') - &GT;引用( 'ID') - &gt;在( '司机');它的工作