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

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"

The problem is probably on this line:

$user->retailer_code = $retailer_code;

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

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

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

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

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

will returns the retailer_code so

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

Please refer this link.