2014-11-02 122 views
0

这是我的代码:变量函数无法识别,laravel

public function userprofile($id) 
{ 
    $users = DB::table('users')->where('username', $id)->get(); 

    $myposts = DB::table('posts')->where('maker', $id)->get(); 

    $user_id = $id; 

    $postsvotes = DB::table('posts') 
    ->join('upvotes', function($join) 
    { 
     $join->on('post_id', '=', 'upvotes.post_id') 
      ->where('upvotes.user_id', $user_id); 
    }) 
    ->get(); 

    return View::make('users.profile')->with('users', $users)->with('myposts', $myposts)->with('myvotes', $postsvotes); 
} 

我的工作一加入,因为你可以在看,我有这个变量$ user_ID的,这应该是$ ID你从函数userprofile($ id)得到一个参数,但我似乎无法将该参数放入where子句中。

问候,

回答

2

继续前进,并尝试(注意使用($ user_ID的)部分):

->join('upvotes', function($join) use ($user_id) 
{ 
    $join->on('post_id', '=', 'upvotes.post_id') 
     ->where('upvotes.user_id', $user_id); 
}) 

这可能是一个变量范围的问题。见示例#3闭包和范围确定http://php.net/manual/en/functions.anonymous.php