duanping6698 2015-05-08 06:20
浏览 127

Laravel调用方法一旦模型加载

I am trying to check in the constructor of a model if the currently authenticated user is allowed to access the given model, but I am finding that $this from the constructor's context is empty. Where are the attributes assigned to a model in Laravel and how should I go about calling a method once all of the attributes have been loaded?

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    var_dump($this); // empty model
    $this->checkAccessible();

}

Cheers in advance

  • 写回答

1条回答 默认 最新

  • dongyin8991 2015-05-08 06:55
    关注

    you can use controller filter to check whether user logged in or not and than you call any model function.

    public function __construct(array $attributes = []){     
      $this->beforeFilter('auth', array('except' => 'login')); //login route
    
       if(Auth::user()){
            $user_id = Auth::user()->user_id;
            $model = new Model($attributes);
            //$model = User::find($user_id);
       }
     }
    

    Binding Attributes to Model from constructor

    Model.php

    public function __construct(array $attributes = array())
    {
        $this->setRawAttributes($attributes, true);
        parent::__construct($attributes);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大