简体   繁体   中英

How can i fetch another table data in different table column?

Firstly i have given all three table structure.

actions table: actionsTable

roles table: rolesTable

permissions table: 在此输入图像描述

Here how can i get action_id in permissions table from actions table? and how can i get role_id in permissions table from roles table? Please tell me the easy way to do , i am beginner in Laravel.

Action Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Action extends Model
{
    //
    protected $table = "actions";
    //public $fillable = []


    public function role(){
        return $this->belongsTo('App\Action');
    }    
    public function permission(){
        return $this->belongsTo('App\Action');
    }
}

Permission Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    //
    protected $table ="permissions";

    public function actionGet(){
        return $this->hasOne('App/Permission');
    }
}
update permission_table a join action_table b on a.id = b.id join roles_table c
on a.id = c.id
set a.action_id = b.id,
a.role_id = c.id;

This will update action_id in permission table with id from action table also, role_id in permission table with id from role table. I assume this is what you want.

I have found a way to do this work.I am using for this Query Builder to insert actions table id in permissions table action_id column.

For this, in RoleController:

public function store(Request $request)
{
    //
    $role = [];
    $role['role'] = $request->input('role');

   $data= Role::create($role);
   $id= $data->id;
   DB::table('permissions')->insert([
    'role_id' => $id
    ]);
    return redirect(route('allRole'));
}

And ActionController:

public function store(Request $request)
{
    //
    $action= [];
    $action['action'] = $request->input('action');

   $data= Action::create($action);
   $id= $data->id;
   DB::table('permissions')->insert([
    'action_id' => $id
    ]);
    return redirect(route('allAction'));
}

Before do this add use DB; in your header of each controller.

Hope this will help for someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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