0

This is my code:

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);
}

I'm working with a join and as you can see in the where I have this variable $user_id and this should be the $id you get as an argument from the function userprofile($id) but I can't seem to get that argument into that where clause.

Regards,

1 Answer 1

2

Go ahead and try (notice the use ($user_id) part):

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

This probably is a variable scoping problem. See Example #3 Closures and scoping from http://php.net/manual/en/functions.anonymous.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.