Home >PHP Framework >Laravel >How to query a column of records in a database using Laravel

How to query a column of records in a database using Laravel

PHPz
PHPzOriginal
2023-04-11 15:06:241715browse

Laravel is a modern PHP framework that provides many powerful and simple development tools, making web application development very easy. In Laravel, we can use the ORM (Object Relational Mapping) mode to access the database without manually writing SQL statements.

Querying a column in the database is one of the common operations in Laravel. In this article, we will explore how to query a column in a database using Laravel.

Step 1: Create a database

Before we start using Laravel to query the database, we need to first create a database. Any database can be used, but in this article we will use the MySQL database.

Step 2: Configure the database connection

In Laravel, we can store the database connection configuration in the .env file. Open the .env file and find the following line:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Make sure the configuration values ​​are correct and that the database the project is connected to is configured correctly.

Step 3: Create a database table

After completing the database connection configuration, let us create a database table for our application. In this example, we will create a users table. Use the following command to create the table:

php artisan make:migration create_users_table --create=users

After running the above command, Laravel will create a users table migration file for you. Define your data table structure in this file. In this example, the data table schema is as follows:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

In this example, we have added a name column that stores the user's name. We will use this column to perform the query.

Run the following command to run the migration:

php artisan migrate

Step 4: Query a column using Laravel

After completing the above steps, we can now query a column in the database using Laravel. We can do this using Laravel's query builder.

In this example, we will use the following code to execute the query:

$users = DB::table('users')->pluck('name');

This code retrieves the name column from the users table. pluckThe method returns an array containing the column values.

You can also use Laravel's models to query a column in the database. The following code demonstrates how to do this using a model:

$users = User::pluck('name');

This code uses the User model from the users table, and from the name column Retrieve value.

Conclusion

Querying a column of a database table is one of the regular database operations in Laravel. In this article, we explored how to retrieve a column from a database table using Laravel's query builder and models. With these examples, you can better understand how the Laravel ORM interacts with the database.

The above is the detailed content of How to query a column of records in a database using Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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