简体   繁体   English

Laravel 4:使用$ this和未定义的变量:user_id

[英]Laravel 4: Using $this and Undefined variable: user_id

I'm new with Laravel and I have some problem 我是Laravel的新手,遇到了一些问题

First Problem: When I using $this-> in __construct function , and want to using it in a method Like this code: 第一个问题:当我在__construct函数中使用$ this->,并想在类似于以下代码的方法中使用它时:

// construct method
public function __construct() {
        $this->user_id = Auth::user()->id_user;
}
// another method 
    public function index()
    {


        $this->data['files'] = DB::table('mhd_cloud_transfer_files')->distinct()
                    ->where('mhd_cloud_transfer_files.to_transfer_file',$this->user_id )
                    ->where('mhd_cloud_transfer_files.folder_transfer_file',0)
                    ->orwhere(function($query){
                        $query->where('from_transfer_file', $this->user_id );
                        $query->where('to_transfer_file',0);
                    })
                    ->join('mhd_cloud_files', 'mhd_cloud_files.id_file', '=','mhd_cloud_transfer_files.id_file')
                    ->join('mhd_main_users', 'mhd_main_users.id_user', '=', 'mhd_cloud_files.author_file')
                    ->paginate(25);
 }

this error : 这个错误:

ErrorException: Undefined variable: user_id ErrorException:未定义的变量:user_id

and another problem when I'm using $this-> in methods 当我在方法中使用$ this->时的另一个问题

in same problem this error: 在同一问题中,此错误:

Using $this when not in object context 不在对象上下文中时使用$ this

This is All of my code (controller class) http://pastebin.com/eVbuknLd 这是我所有的代码(控制器类) http://pastebin.com/eVbuknLd

try adding public var $user_id to the class declaration. 尝试将public var $user_id添加到类声明中。

just remember to check Auth::user() for a NULL that indicates no user is logged in. unless you are sure you check it anywhere you use this class. 只需记住检查Auth::user()是否有NULL (表示没有用户登录)即可。除非您确定在使用此类的任何地方都进行了检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM