2016-04-19 38 views
1

首先我給出了所有三種表格結構。我怎樣才能獲取不同的表列中的另一個表數據?

動作表: actionsTable

角色表: rolesTable

權限表: enter image description here

在這裏,我怎麼能在permissions表中獲取action_idactions表? 以及如何從roles表中獲得role_idpermissions表?請告訴我簡單的方法,我是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'); 
    } 
} 
+0

你的意思是將action_table中的action_id數據插入權限表? – Priyanshu

+0

是的,當我有'行動'表'id'然後我想插入數據''action_id''權限'表。 –

+0

「請告訴我簡單的方法」 - 嚴重的是,如果您想學習,請停止尋找簡單的方法。 – ggdx

回答

0

我已經找到一種方法來做到這一點的幹活。現在用這個Query Builder插入actionsidpermissionsaction_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')); 
} 

之前執行此添加use DB;在你的每個控制器的頭。

希望這會有助於某人。

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

這將更新許可表的action_id從動作表 也ID,ROLE_ID在允許錶帶有來自角色表的ID。 我認爲這是你想要的。

+0

好吧,我正在嘗試,讓你知道發生了什麼事。我會用哪種模式做?我怎麼能在控制器中調用這個? –

+0

它是一個用於更新權限表中的role_id和action_id的mysql查詢。 正如你使用phpMyadmin,應該有一個SQL選項卡。 打開它並運行這個查詢 – Priyanshu

+0

我這是SQL查詢,但這只是一次顯示結果。但我想當我有'行動'表中'id'那麼我也''action_id'填寫'權限'表,並將這些數據保存在'action_id'列中。 –

相關問題