简体   繁体   English

如何在laravel中插入一对多关系?

[英]how to insert one to many relation in laravel?

this is navigation schema这是导航模式

    Schema::create('navigations', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });

this is sub navigation schema这是子导航模式

    Schema::create('sub_navigations', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('nav_id')->unsigned()->index();
        $table->string('name');
        $table->timestamps();
        $table->foreign('nav_id')->references('id')->on('navigations')->onDelete('cascade');

    });

this is navigation model这是导航模型

public function subnavigations(){
    return $this->hasMany('App\Model\Admin\Sub_navigation');
}

this is sub navigation model这是子导航模型

public function navigation(){
    return $this->belongsTo('App\Model\Admin\Navigation');
}

controller's code控制器代码

public function store(Request $request)
{
    //return $request->all();
    $sub_navigation = new Sub_navigation;

    $sub_navigation->name = $request->name;
    $sub_navigation->nav_id = $request->navigation[0];
    //return $sub_navigation;
    //$sub_navigation->save();
    $navigation = Navigation::find($request->navigation[0]);
    // $navigation->subnavigations()->save($sub_navigation);
    //return $navigation;
    $sub_navigation->navigation()->associate($navigation);
    $sub_navigation->save();






    return redirect()->back();

}

this is error message这是错误信息

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'navigation_id' in 'field list' (SQL: insert into `sub_navigations` (`name`, `nav_id`, `navigation_id`, `updated_at`, `created_at`) values (Philosophy, 2, 2, 2019-11-25 02:17:33, 2019-11-25 02:17:33))

From the docs:从文档:
One To Many 一对多

Remember, Eloquent will automatically determine the proper foreign key column on the Sub_navigation model.请记住,Eloquent 会自动确定Sub_navigation模型上正确的外键列。 By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id .按照惯例,Eloquent 将采用拥有模型的“蛇形案例”名称并为其添加后缀_id So, Eloquent will assume the foreign key on the Sub_navigation model is navigation_id .因此,Eloquent 将假设Sub_navigation模型上的外键是navigation_id You can override the foreign key by passing additional argument to the hasMany method:您可以通过将附加参数传递给hasMany方法来覆盖外键:

/**
 * Get the subnavigations for the navigation.
 */
public function subnavigations(){
    return $this->hasMany('App\Model\Admin\Sub_navigation', 'nav_id');
}

One To Many (Inverse) 一对多(逆)

Eloquent will try to match the navigation_id from the Sub_navigation model to an id on the Navigation model.洋洋洒洒将尝试在比赛navigation_idSub_navigation模型对一个id Navigation模式。 Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with a _ followed by the name of the primary key column. Eloquent 通过检查关系方法的名称并在方法名称后加一个_后跟主键列的名称来确定默认外键名称。 However, if the foreign key on the Sub_navigation model is not navigation_id , you may pass a custom key name as the second argument to the belongsTo method:但是,如果Sub_navigation模型上的外键不是navigation_id ,您可以将自定义键名称作为第二个参数传递给belongsTo方法:

/**
 * Get the navigation that owns the sub_navigation.
 */
public function navigation()
{
    return $this->belongsTo('App\Model\Admin\Navigation', 'nav_id');
}

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

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