繁体   English   中英

Laravel插入3个相关表

[英]Laravel insert to 3 related tables

嗨,大家好,我想知道是否有人尝试使用3个相关表将记录插入laravel中的表?

// 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个相关表中。 有人可以帮我弄这个吗。 谢谢

尝试这个

$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",            
]);

当您执行此代码时

$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",            
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM