Laravel is a popular PHP development framework, in which querying the database is one of the frequently used operations. In the actual development process, sometimes it is necessary to loop through the query results in order to display the results or further process them. This article will introduce how to loop query results in Laravel.
1. Query data
Before starting to loop to query the results, you need to query the data first. Laravel provides two query methods: Eloquent ORM and Query Builder. Eloquent ORM is an object-relational mapping that can map tables in the database into objects, making it easy to operate the database. Query Builder is also a query method. It is a builder pattern that builds SQL query statements through chain calls.
Let’s introduce how to use Eloquent ORM and Query Builder to query data.
(1) Eloquent ORM query data
When using Eloquent ORM to query data, you need to define a corresponding Model first. Model corresponds to the table in the database. We can define fields, related tables, data operations and other methods in the Model.
Define Model Example:
namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { // 定义对应表的名称 protected $table = 'user'; // 定义主键名称 protected $primaryKey = 'id'; // 定义可被批量赋值的字段 protected $fillable = [ 'name', 'age', 'gender' ]; // 定义关联关系等方法 }
After defining the Model, you can use Eloquent ORM to query.
Query all data:
$users = App\Models\User::all();
Query one piece of data:
$user = App\Models\User::find(1);
Query multiple pieces of data based on conditions:
$users = App\Models\User::where('age', '>', 18)->get();
What needs to be noted here is that Eloquent ORM The result of the query is a Collection object, not an array. Collection objects provide many convenient methods to help us process data.
(2) Query Builder to query data
To use Query Builder to query data, we directly call the method of the DB class. When using Query Builder, you need to first call the table method of the DB class to specify the query table, and then you can build an SQL query statement through chain calls.
Query all data:
$users = DB::table('user')->get();
Query one piece of data:
$user = DB::table('user')->where('id', '=', 1)->first();
Query multiple pieces of data based on conditions:
$users = DB::table('user')->where('age', '>', 18)->get();
2. Loop through the query results
After querying the data, we can loop through the query results. You can use a foreach loop or a for loop to traverse.
(1) Using foreach loop
When using foreach loop, you can directly loop through the query results (Collection object queried by Eloquent ORM or stdClass object queried by Query Builder).
Eloquent ORM Example:
$users = App\Models\User::all(); foreach($users as $user) { echo $user->name; }
Query Builder Example:
$users = DB::table('user')->get(); foreach($users as $user) { echo $user->name; }
(2) Using for loop
When using for loop, you need to first convert the query results Convert it to an array and then traverse it.
Eloquent ORM Example:
$users = App\Models\User::all()->toArray(); $total = count($users); for($i = 0; $i < $total; ++$i) { echo $users[$i]['name']; }
Query Builder Example:
$users = DB::table('user')->get()->toArray(); $total = count($users); for($i = 0; $i < $total; ++$i) { echo $users[$i]->name; }
Summary
In this article, we introduced both Eloquent ORM and Query Builder in Laravel There are several ways to query data, and sample code for traversing using foreach loops and for loops is provided. In the actual development process, you can choose the appropriate method for data query and traversal according to specific business needs.
The above is the detailed content of Detailed explanation of the method of looping query results in Laravel. For more information, please follow other related articles on the PHP Chinese website!