dongyied24121
dongyied24121
2015-02-10 15:04

语法错误,Laravel中的意外' - >'(T_OBJECT_OPERATOR)

已采纳

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条回答

  • douli1854 douli1854 6年前

    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) {
    
    点赞 评论 复制链接分享
  • doupeng8419 doupeng8419 6年前

    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));
            })
    
    点赞 评论 复制链接分享