如何在控制器内使用constructer进行函数重定向

时间:2018-03-27 08:06:44

标签: php laravel-5 laravel-5.2

在larvel中,我在我的控制器上创建了构造函数,如:

public function __construct()
    {
      $perm=permission_Byid(Session::get('userid'));
       foreach ($perm as $key => $value) {
        if(in_array($value->permission_id,'4'))
        {

          echo "ok";exit;

        }
        else
        {
          echo "no";exit;

        }
       }
    }

在$ perm中我得到的数据如下:

Array
(
    [0] => stdClass Object
        (
            [permission_id] => 1
            [admin_permission_field] => Access Admin Page
        )

    [1] => stdClass Object
        (
            [permission_id] => 2
            [admin_permission_field] => Print/Export Log
        )

    [2] => stdClass Object
        (
            [permission_id] => 3
            [admin_permission_field] => Approve/Decline Request
        )

    [3] => stdClass Object
        (
            [permission_id] => 4
            [admin_permission_field] => Edit Lists
        )

    [4] => stdClass Object
        (
            [permission_id] => 5
            [admin_permission_field] => Edit Agent Info
        )

    [5] => stdClass Object
        (
            [permission_id] => 6
            [admin_permission_field] => Edit Listings
        )
)

我希望当我得到[permission_id] => 4如果没有permission_id = 4则调用我的控制器另一个函数它会向我显示错误页面的视图页面。

在可能的情况下,如果和其他条件不起作用..

在heleper我的功能如下:

function permission_Byid($userid)
    {
        return DB::table('tbl_admin_permission as tap')
                                        ->select('tap.permission_id as permission_id','taa.permission_eng as admin_permission_field')
                                         ->join('tbl_admin_users as tau', 'tau.id', '=', 'tap.admin_id')
                                        ->join('tbl_admin_access as taa', 'taa.id', '=', 'tap.permission_id')
                                        ->where('tap.admin_id','=',$userid)
         ->get();

    } 
你能帮我解决这个问题吗?我会非常沮丧

3 个答案:

答案 0 :(得分:0)

我认为无需查看in_array

例如,

public function __construct()
{
    $perm=permission_Byid(Session::get('userid'));

    foreach ($perm as $key => $value) {
        if ($value->permission_id == '4') {
            echo "ok";exit;
        } else {
            echo "no";exit;
        }
    }
}

答案 1 :(得分:0)

您的$value->permission_id不是数组,因此您无法使用in_array来检查该值。

所以更换一行

if(in_array($value->permission_id,'4'))

if($value->permission_id == 4)

答案 2 :(得分:0)

if (sizeof(array_filter($perm, function($value) {
    return $value->permission_id === 4;
}))) {
   echo "ok";exit;
} else {
     echo "no";exit;
}