1

I have the following sql queries in my application.

$defaultGoal = DB::table("goals")
-> where("activitiesID", "=", $this->activityID)
-> where("usersID", "=", $userID)
-> pluck("goal");

$defaultGoalPoints = DB::table("goals")
-> where("activitiesID", "=", $this->activityID)
-> where("usersID", "=", $userID)
-> pluck("goalpoints");

$defaultPoints = DB::table("goals")
-> where("activitiesID", "=", $this->activityID)
-> where("usersID", "=", $userID)
-> pluck("points");

Each query returns an array. Is there a way to do combine all of these queries into one, that will return the same arrays for $defaultPoints, $defaultGoalPoints, and $defaultGoal.

1
  • If the goal is to reduce the number of queries you're writing, what about simply using ->get() to retrieve the entire entries and then using php to pull the data you want into arrays? (Laravel may have a shorter way to do this though, and I'll let someone else speak up if they know of it.) Commented Apr 14, 2016 at 19:05

1 Answer 1

1

You can use select to get only what you need:

$defaults = DB::table("goals")
->select('goal', 'goalpoints', 'points')
->where("activitiesID", "=", $this->activityID)
->where("usersID", "=", $userID)
->get();

Hope it helps...

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.