本文介绍了Laravel插入3个相关表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi guys I would like to know if anyone of you have tried inserting a record into a table in laravel using 3 related tables? Example

// 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下保存此

Now I tried saving this using this code under 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个相关表中.有人可以帮我弄这个吗.谢谢

Dont know how to save this is just one saving. I would like to save the records in the 3 related tables. Can someone help me with this. Thanks

推荐答案

在执行此代码时

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

实际上没有 pharmacy_id 的数据,这就是问题所在.因此,您应该更改 pharmacy_id 的架构,并将其值设置为 default 0

There is actually no data for pharmacy_id so that is the problem. So you should either change your schema for pharmacy_id and set it's value as default 0

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

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

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

这篇关于Laravel插入3个相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 08:31