我怎样才能在不同的 table 列中获取另一个 table 数据?

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

首先我给出了所有三个 table 结构。

操作table:

角色table:

权限table:

这里如何从 actions table 中获取 permissions table 中的 action_id? 以及如何从 roles table 中获取 permissions table 中的 role_id?请告诉我简单的方法,我是 Laravel.

的初学者

动作模型:

<?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');
    }
}

权限模型:

<?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;

这将使用操作 table 中的 ID 更新 action_id 权限 table 此外,role_id 获得权限 table,ID 来自角色 table。 我想这就是你想要的。

我找到了一种方法 work.I 我正在使用 Query Builder 插入 actions table idpermissions table action_id 列中。

为此,在 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'));
}

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'));
}

在执行此操作之前,在每个控制器的 header 中添加 use DB;

希望这对某人有所帮助。