laravel 对象无法转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24207622/
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 09:38:47  来源:igfitidea点击:

Object could not be converted to strings

jquerysessionlaravel

提问by Hobby99

My query causes: Object of class Illuminate\Database\Eloquent\Builder could not be converted to string. When I replace $unamewith a hardcoded name 'Dennis'in the model-query then the error disappears. I checked whether the session variable ('username') was empty and causing the error but it is set. Thanks for any hint.

我查询的原因:Object of class Illuminate\Database\Eloquent\Builder could not be converted to string。当我在模型查询$uname中用硬编码名称替换时,'Dennis'错误就会消失。我检查了会话变量 ( 'username')是否为空并导致错误,但它已设置。感谢您的任何提示。

Controller:

控制器:

$username = Session::get('username');
$data['users'] = Profile::GetUserData($username);
$users = Profile::GetUserData();

return View::make('profile_view', $data);

Model:

模型:

class Profile extends Eloquent{
    public function scopeGetUserData($uname){
        return DB::table('users')->where('user_name', '=', $uname)->first();
        //return DB::table('users')->where('user_name', '=', 'Dennis')->first();
    }
}

回答by The Alpha

In your Controllerfollowing code is causing the problem because getUserData()expects an argument and you are calling that method without any argument like this:

在您的Controller以下代码中导致问题,因为getUserData()需要一个参数,而您正在调用该方法而没有任何参数,如下所示:

$users = Profile::GetUserData();

The given code above is unnecessary because you are first calling the getUserData()method properly like this:

上面给定的代码是不必要的,因为您首先getUserData()像这样正确调用该方法:

$data['users'] = Profile::GetUserData($username);

Then again calling that method improperly and this line has no use because you are passing the $dataarray to your view, so remove following line:

然后再次错误地调用该方法并且该行没有用,因为您将$data数组传递给您的view,因此删除以下行:

$users = Profile::GetUserData();

As a result your Controllermay contain the code given below:

因此,您Controller可能包含以下代码:

$username = Session::get('username');
$data['users'] = Profile::GetUserData($username)->first();
return View::make('profile_view', $data);

Also in your scope methodyou should use two parameters because first parameter will be injected by Laravelwhich will be the Illuminate\Database\Eloquent\Builderobject and you should return this object instead of executing the query inside the method (using first) so you may chain other methods (if needed):

同样在你的scope method你应该使用两个参数,因为第一个参数将被注入,Laravel这将是Illuminate\Database\Eloquent\Builder对象,你应该返回这个对象而不是在方法内部执行查询(首先使用),所以你可以链接其他方法(如果需要):

class Profile extends Eloquent{
    public function scopeGetUserData($query, $uname){
        return $query->where('user_name', $uname);
    }
}

But you may also call it like this:

但你也可以这样称呼它:

public function scopeGetUserData($query, $uname){
    return $query->where('user_name', $uname)->first();
}

In this case your controller should be like this:

在这种情况下,您的控制器应该是这样的:

$username = Session::get('username');
$data['users'] = Profile::GetUserData($username);
return View::make('profile_view', $data);