1

In Laravel 4.2, I am getting the following error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_PARSE)
syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ')' 

This error happens in class Recommendation, which provides static function getRecommendations

class Recommendation extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'recommendations';

    public static function getRecommendations($userID) {
        $recommendations = DB::table('recommendations')
            ->join('sites', function($join) use ($user->id) {
                $join->on('sites.id', '=', 'recommendations.site_id');
                $join->on('sites.owner', '=', DB::raw($userID));
            })
            ->select('recommendations.id', 'recommendations.title', 'recommendations.body', 'recommendations.site_id', 'site.address')
            ->get();
        return $recommendations;
    }

}

on this line

->join('sites', function($join) use ($user->id) {

I cannot understand, what's wrong with the query..

Structure of Recommendations table is

id  int(10) unsigned Autoincrement   
title   varchar(255)     
body    text     
site_id int(10) unsigned     
created_at  timestamp [0000-00-00 00:00:00]  
updated_at  timestamp [0000-00-00 00:00:00]

and Sites table is

id  int(10) unsigned Автоматическое приращение   
sitename    varchar(255)     
address varchar(64)  
owner   int(10) unsigned     
created_at  timestamp [0000-00-00 00:00:00]  
updated_at  timestamp [0000-00-00 00:00:00]
2
  • 2
    What is your $user->id ? You are not using it anywhere. You only have $userID Commented Feb 10, 2015 at 15:08
  • that's what happens whan you start to panic Commented Feb 10, 2015 at 15:13

2 Answers 2

2

There is no variable $user available in function getRecommendations(), the function argument $userID is not used inside the function and the anonymous function uses $userID.

The only conclusion is that the line should read:

->join('sites', function($join) use ($userID) {
Sign up to request clarification or add additional context in comments.

1 Comment

surely, such simple mistake... sorry for bothering
2

You can't pass a value with use it has to be a local variable.

Either assign it before:

$userId = $user->id;
$recommendations = DB::table('recommendations')
        ->join('sites', function($join) use ($userId) {
            $join->on('sites.id', '=', 'recommendations.site_id');
            $join->on('sites.owner', '=', DB::raw($userId));
        })

Or pass the whole $user:

->join('sites', function($join) use ($user) {
            $join->on('sites.id', '=', 'recommendations.site_id');
            $join->on('sites.owner', '=', DB::raw($user->id));
        })

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.