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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28434971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:53:37  来源:igfitidea点击:

syntax error, unexpected '->' (T_OBJECT_OPERATOR) in Laravel

phpmysqllaravel

提问by Thelambofgoat

In Laravel 4.2, I am getting the following error:

在 Laravel 4.2 中,我收到以下错误:

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

此错误发生在类 Recommendation 中,该类提供静态函数 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 Recommendationstable 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 Sitestable 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]

回答by axiac

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

$user函数中没有可用的变量,函数内部不使用getRecommendations()函数参数$userID,匿名函数使用$userID.

The only conclusion is that the line should read:

唯一的结论是该行应为:

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

回答by lukasgeiter

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

你不能传递一个值,use它必须是一个局部变量

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:

或者通过整个$user

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