dimanche 24 novembre 2019

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 Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2XKKHyU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire