Reputation: 349
In my Laravel application, I try to access the text within the array within only one element, but it gives me error.
$account_id = DB::select('select id from chart_of_accounts where type =\'Expense\' LIMIT '.$request->account_id.',1;');
return $account_id[0];
The error message:
The Response content must be a string or object implementing __toString(), "object" given.
If I return the whole thing:
return $account_id;
Output:
[{"id":19}]
I do not know how can I convert this array into a single string?
Upvotes: 0
Views: 61
Reputation: 497
It's better to use this query method
$account = DB::table('chart_of_accounts')
->select('id')
->where('id', $request->account_id)
->where('type', 'Expense')
->take(1)
->first();
return $account->id;
but for offset :
$account = DB::table('chart_of_accounts')
->select('id')
->where('type', 'Expense')
->offset($request->account_id)
->take(1)
->first();
return $account->id;
Upvotes: 0
Reputation: 3608
The laravel DB::select()
function returns an array of objects, as mentioned in the Laravel documentation:
The
select
method will always return anarray
of results. Each result within the array will be a PHPStdClass
object, allowing you to access the values of the results
You will need to access the id
attribute of the object:
$account_id = DB::select('select id from chart_of_accounts where type =\'Expense\' LIMIT '.$request->account_id.',1;');
return $account_id[0]->id;
Upvotes: 3