Home > PHP Framework > Laravel > body text

Detailed explanation of the method of looping query results in Laravel

PHPz
Release: 2023-04-06 16:53:45
Original
1533 people have browsed it

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'
    ];

    // 定义关联关系等方法
}
Copy after login

After defining the Model, you can use Eloquent ORM to query.

Query all data:

$users = App\Models\User::all();
Copy after login

Query one piece of data:

$user = App\Models\User::find(1);
Copy after login

Query multiple pieces of data based on conditions:

$users = App\Models\User::where('age', '>', 18)->get();
Copy after login

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();
Copy after login

Query one piece of data:

$user = DB::table('user')->where('id', '=', 1)->first();
Copy after login

Query multiple pieces of data based on conditions:

$users = DB::table('user')->where('age', '>', 18)->get();
Copy after login

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;
}
Copy after login

Query Builder Example:

$users = DB::table('user')->get();
foreach($users as $user) {
    echo $user->name;
}
Copy after login

(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][&#39;name&#39;];
}
Copy after login

Query Builder Example:

$users = DB::table(&#39;user&#39;)->get()->toArray();
$total = count($users);
for($i = 0; $i < $total; ++$i) {
    echo $users[$i]->name;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template