0
我有bican角色插件,我需要通過數據透視表訪問。這是在我的控制器代碼如何訪問數據透視表列
$users = User::where('role_id','=',2)->get();
運動中的作用
Schema::create('roles', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('slug')->unique();
$table->string('description')->nullable();
$table->integer('level')->default(1);
$table->timestamps();
});
遷移用戶
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('email')->unique();
$table->rememberToken();
$table->timestamps();
});
樞遷移
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('role_id')->unsigned()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
一個問題,whereHas着眼於數據透視表? – OunknownO
'whereHas'用於查詢關係。你可以閱讀這裏的文檔(https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence)。 –