2015-08-09 45 views
1

嗨,大家好,我想知道你们中的任何一个人是否尝试过使用3个相关的表格在laravel的表格中插入一条记录?例如Laravel插入3个相关表格

// Database Schema for Pharmacy 
Schema::create('pharmacies', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->text('address'); 
}); 

// Relationship in App\Pharmacy table 
public function pharmacists() { 
    return $this->hasMany('App\Pharmacist'); 

} 

现在我有另一个表

Schema::create('pharmacists', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('pharmacy_id')->unsigned()->index(); 
     $table->foreign('pharmacy_id')->references('id')->on('pharmacies')->onDelete('cascade'); 
     $table->string('fname'); 
}); 

// And this is the relationship in App\Pharmacist class 
public function account() { 
    return $this->hasOne('App\Account'); 
} 

public function pharmacy() { 
    return $this->belongsTo('App\Pharmacy'); 
} 

现在第三台

// This contain the foreign key for pharmacist_id 
Schema::create('accounts', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('pharmacist_id')->unsigned()->index(); 
     $table->foreign('pharmacist_id')->references('id')->on('pharmacists')->onDelete('cascade'); 
     $table->string('username')->unique(); 
}); 

// This is the relationship in App\Account class 
public function pharmacists() { 
    return $this->belongsTo('App\Pharmacist'); 
} 

现在我试图AccountsController下

$input = Request::all(); 

    $pharmacy = Pharmacy::create([ 
        "name" => "Wendies Chicken", 
        "address" => "My Address",       
       ]); 

    $pharmacists = new Pharmacist([ 
     "fname" => "Administrator",    
    ]); 

    $account = new Account([ 
     "username" => "root",    
    ]); 

    $pharmacists->account()->save($account); 
    $pharmacy->pharmacists()->save($pharmacists); 

保存这个使用此代码但我收到了一封电子邮件恐怖说

Integrity constraint violation: 1048 Column 'pharmacist_id' cannot be null (SQL: insert into `accounts` (`username`, `pharmacist_id`, `updated_at`, `created_at`) values (root, 2015-08-09 05:13:31, 2015-08-09 05:13:31)) 

不知道如何保存这只是一个节省。我想保存3个相关表中的记录。有人可以帮我弄这个吗。由于

回答

0

当您执行此代码

$pharmacists->account()->save($account); 
$pharmacy->pharmacists()->save($pharmacists); 

居然没有任何数据pharmacy_id所以这就是问题所在。所以,你应该要么改变你的架构pharmacy_id,并设置它的值作为default 0

$pharmacy = Pharmacy::create([ 
       "name" => "Wendies Chicken", 
       "address" => "My Address",       
      ]); 

$pharmacy->pharmacists()->save([ 
    "fname" => "Administrator",    
]); 

$pharmacy->pharmacists()->account()->save([ 
    "username" => "root",    
]); 
+0

此问题解决了非常感谢,虽然我这样做,而不是$ pharmacy->药剂师() - >保存($药师) - >账户() - >保存($帐户);它似乎也起作用。谢谢你 –

0

试试这个

$input = Request::all(); 

$pharmacy = Pharmacy::create([ 
    "name"    => "Wendies Chicken", 
    "address"   => "My Address",       
]); 

$pharmacists = Pharmacist::create([ 
    "pharmacy_id"  => $pharmacy->id, 
    "fname"    => "Administrator", 
]); 

$account = Account::create([ 
    "pharmacist_id"  => $pharmacists->id 
    "username"   => "root",    
]);