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

时间:2014-09-26 06:52:32

标签: php mysql database laravel

当我运行下面的代码时

$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!');

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

3 个答案:

答案 0 :(得分:2)

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

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

将返回零售商代码

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

请参阅此link

答案 1 :(得分:1)

问题可能出在这一行:

$user->retailer_code = $retailer_code;

$retailer_code是一个对象,你需要得到它的属性:

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

答案 2 :(得分:0)

你应该改变:

$user->retailer_code = $retailer_code;

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

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

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

然后使用:

$user->retailer_code = $retailer_code;

没有变化