laravel 错误:类 stdClass 的对象无法转换为字符串

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

ERROR: Object of class stdClass could not be converted to string

phpmysqldatabaselaravel

提问by Lumiere

When I run the code below

当我运行下面的代码时

$id = Input::get('branch_id');
$retailer_code = DB::table('branches')->select('retailer_code')->where('id', $id)->first();
$user = new User;
$user->user_firstname = Input::get('user_firstname');
$user->user_lastname = Input::get('user_lastname');
$user->user_email = Input::get('user_email');
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->position_id = Input::get('position_id');
$user->retailer_code = $retailer_code;
$user->branch = Input::get('branch_code');
$user->status = "1";
$user->save();

return Redirect::to('admin/users')->with('message', 'New User Added!');

I get this error "Object of class stdClass could not be converted to string"

我收到此错误“无法将类 stdClass 的对象转换为字符串”

采纳答案by lowerends

The problem is probably on this line:

问题可能出在这一行:

$user->retailer_code = $retailer_code;

$retailer_codeis an object and you need to get the property of it like this:

$retailer_code是一个对象,您需要像这样获取它的属性:

$user->retailer_code = $retailer_code->retailer_code;

回答by Wit Wikky

$retailer_code which is stdClass and you cannot use directly as variable ,

$retailer_code 是 stdClass,不能直接用作变量,

$retailer_code = DB::table('branches')->select('retailer_code')->where('id', $id)->first();

will returns the retailer_code so

将返回retail_code 所以

$retailer_code->retailer_code //would be the solution.

Please refer this link.

请参考此链接

回答by Marcin Nabia?ek

You should change:

你应该改变:

$user->retailer_code = $retailer_code;

into

进入

$user->retailer_code = $retailer_code->retailer_code;

or you could modify your query:

或者您可以修改您的查询:

$retailer_code = DB::table('branches')->where('id', $id)->pluck('retailer_code');

and then use:

然后使用:

$user->retailer_code = $retailer_code;

without a change

没有改变