2

$arr = array('field1', 'field2', 'field3');

From User Inputs I get array containing Field names. I have to fetch data from multiple table joins so, The goal is to select respective fields from tables according to table input from array.

    $update_co_column = Co_Total_Ia::
                      ->select(current($arr), next($arr), next($arr))
                     //->join('All Joins here')
                       ->where("user_key", "=", session()->get("user_id"))
                       ->where("student_details.deleted_at", "=", null);

I tried using PHP array methods, curret(arr) and next(arr). It is working, but just as an temporary solution. Problems in current solution -

  • Don't know exact how many fields to fetch
  • Need to fetch extra useless field all times.

1 Answer 1

2

You can do it like this:

$fields = ['field1', 'field2', 'field3'];

Model::select($fields)->get();

results:

SELECT
  `field1`,
  `field2`,
  `field3`
FROM
  `model`
Sign up to request clarification or add additional context in comments.

Comments

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.