0

Assume I have a simple laravel query builder.

$data = DB::table('users')->get();

(I want to use only query builder and not eloquent since the query will be very complex in reality)

and the table fields are userid, firstname, lastname, gender

before I pass the $data to my view I would like to modify the output data using PHP

Say I wanted to add the prefix Mr or Miss to firstname depending on the gender column or something like that.. (please don't suggest a pure sql solution since the complexity of the condition will be much more complex that just adding a prefix.. I had given this as a simple use case only)

Is it possible ?

1

1 Answer 1

1

just iterate the result

foreach ($data as $key => $value) {
  if ($value->gender === 1) {
    $value->lastname = 'Mr ' . $value->lastname;
  } else if ($value->gender === 0) {
    $value->lastname = 'Miss ' . $value->lastname;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

forgot the obvious :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.