I am working on a website that uses PHP and SQL to connect to the database, I’m trying to replace all the SQL with Laravel Query Builder commands.
I am running into a problem with the following code:
public function queryUsers() { $db = $this->getMysql(); $stmt = $db->query("SELECT * FROM users"); while ($row = $stmt->fetch()) { $this->addedUsers[$row['user_id']] = $row["slack_handle"]; $this->addedUserNames[$row['user_id']] = $row["name"]; } }
I can easily connect to the database using Laravel, however I am not sure how to continue after the code shown below:
public function queryUsers() { $stmt = DB::table('users')->get(); }
TLDR: In PHP/PDO Fetch fetches the next row from a result set, I’m curious if Laravel has an easy way to do the same.
Advertisement
Answer
public function queryUsers() { $users = DB::table('users')->get(); // $users is array of Std Object. foreach($users as $key => $user) { $data['user_id'] = $user->id; // $user->name, $user->address } }