1

Good day all !!

I have laravel query builder array. I want to set custom value to it.

e.g.

$data = DB::table("table_name")->get();

$data->layout = 'something';
OR 
$data['layout'] = 'something';

Can we do this ? IF yes, then how, because right now I am getting error.

2 Answers 2

2

You need to get record from a collection or an array first and then add custom data:

$data = DB::table("table_name")->get();
$data[0]->layout = 'something';

Or:

$data = DB::table("table_name")->first();
$data->layout = 'something';
Sign up to request clarification or add additional context in comments.

Comments

0

For array access:

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table("table_name")->get();
$data[0]['layout'] = 'something';

OR

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table("table_name")->first();
$data['layout'] = 'something';

For object access:

$data = DB::table("table_name")->get();
$data[0]->layout = 'something';

OR

$data = DB::table("table_name")->first();
$data->layout = 'something';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.