Laravel 5.2:Entrust:调用未定义的方法attachPermission

时间:2016-03-09 07:59:37

标签: php laravel laravel-5.2

我尝试附加权限(如果不存在)并在运行db seed -

后出错
<?php

use Illuminate\Database\Seeder;
use App\Role;
use App\Permission;
use App\User;

class AttachRolesAndPermissionsToUsers extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $admin = Role::where('name', 'admin')->first();
        $moderator = Role::where('name', 'moderator')->first();

        $createPost = Permission::where('name', 'create-post')->first();
        $editUser = Permission::where('name', 'edit-user')->first();


        // attach role
        $adminUser = User::where('email', 'a@gmail.com')->first();
        if(!$adminUser->hasRole('admin')) {
            $adminUser->attachRole($admin);
        }

        $moderatorUser = User::where('email', 'b@gmail.com')->first();
        if(!$moderatorUser->hasRole('moderator')) {
            $moderatorUser->attachRole($moderator);
        }

        // attach Permission
        if(!$adminUser->can(['create-post', 'edit-user'])) {
            $adminUser->attachPermissions([$createPost, $editUser]);
        }

        if(!$moderatorUser->can('create-post')) {
            $moderatorUser->attachPermission($createPost);
        }
    }
}

[BadMethodCallException]调用未定义的方法Illuminate \ Database \ Query \ Builder :: attachPermission()

2 个答案:

答案 0 :(得分:0)

将其更改为

mysql> grant select, update on db.* to 'test'@'???????' identified by 'testpw'

让我们看看它是怎么回事。

答案 1 :(得分:0)

是错误的实施,需要用角色附加权限而不是用户。

[WebMethod]
    public static ChartOutput GetChartData()
    {

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
        {
            SqlCommand cmd = new SqlCommand("Usp_Getdata3", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<ChartDetails> dataList = new List<ChartDetails>();

            foreach (DataRow dtrow in dt.Rows)
            {
                ChartDetails details = new ChartDetails();

                details.WeekNumber = Convert.ToInt16(dtrow[0]);
                details.DistributorName = dtrow[1].ToString();
                details.Count = Convert.ToUInt16(dtrow[2]);

                dataList.Add(details);
            }

            ChartOutput output = new ChartOutput();

            output.Data = new List<int[]>();
            output.Columns = new List<string>();
            var orderedListOfDistributorNames = dataList.Select(x => x.DistributorName).Distinct().OrderBy(x => x).ToArray();


            output.Columns.Add("WeekNumber");
            output.Columns.AddRange(orderedListOfDistributorNames);


            var weekGrouping = dataList.GroupBy(k => k.WeekNumber, v => v);

            foreach (var g in weekGrouping)
            {
                var n = output.Columns.Count;
                int[] dataRow = new int[n];
                var currentWeekNumber = g.Key;

                dataRow[0] = currentWeekNumber;

                for (int i = 0; i < orderedListOfDistributorNames.Count(); i++)
                {
                    var distrtibutorToCheck = orderedListOfDistributorNames[i];

                    var distributorFromGrouping = g.FirstOrDefault(x => x.DistributorName == distrtibutorToCheck);
                    if (distributorFromGrouping != null)
                        dataRow[i + 1] = distributorFromGrouping.Count;

                }

                output.Data.Add(dataRow);
            }

            return output;
        }
    }

    public class ChartOutput
    {
        public List<string> Columns { get; set; }
        public List<int[]> Data { get; set; }
    }

    public class ChartDetails
    {
        public string DistributorName { get; set; }
        public int WeekNumber { get; set; }
        public int Count { get; set; }
    }

$admin->attachPermissions([$createPost, $editUser]); $moderator->attachPermission($createPost); $admin是角色对象。