这是我的代码:
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 ,这应该是您从函数 userprofile($id) 中作为参数获得的 $id 但我似乎看不到将该参数放入 where 子句中。
问候,
最佳答案
继续尝试(注意use ($user_id)部分):
->join('upvotes', function($join) use ($user_id)
{
$join->on('post_id', '=', 'upvotes.post_id')
->where('upvotes.user_id', $user_id);
})
这可能是一个变量范围问题。请参阅 http://php.net/manual/en/functions.anonymous.php 中的示例 #3 闭包和范围界定
关于php - Laravel 函数中无法识别变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702343/