Home > PHP Framework > Laravel > body text

How to query a database in Laravel

PHPz
Release: 2023-04-07 17:24:22
Original
1400 people have browsed it

Laravel is a very popular open source PHP web application development framework. It provides many useful features that make developing web applications easy and fast. One of the most commonly used functions is handling database operations. Databases are at the heart of most web applications. Especially in today's data-driven Internet era, database query is an essential operation. In this article, we will learn how to query a database in Laravel.

Connect to the database

To query the database in Laravel, you first need to connect to the database. In Laravel, you can set up a database connection through a configuration file. Open the config/database.php file and you will see an array that contains different types of database connection configurations. By default, Laravel uses SQLite as the database, if you don't need to switch to another database, you don't need to change this configuration file. However, in this article, we will change to a MySQL database.

Change the driver in the directory in the config/database.php file to 'mysql'.

'default' => env('DB_CONNECTION', 'mysql'),
Copy after login

In the same file, you need to fill in the details required for the database connection. Instead of hardcoding this information into the config/database.php file, you can use an .env file to save this information. Open the .env file and add the following content:

DB_HOST=localhost
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Copy after login

Run the following command to clear the cache:

php artisan config:cache
Copy after login

Create a model

In Laravel, you can use models to interact with the database Interaction. Models are the most common way to access relational databases in Laravel. Using models, query the database just like normal classes. In Laravel, creating models is very simple. Open a command terminal and enter the following command in your application root directory:

php artisan make:model YourModelName
Copy after login

This will create a new model in the app directory.

Query a piece of data

Now, let us take a look at how to use the Laravel model to query a piece of data in the database.

Create a route in your application that will call a controller method. In the controller method, you need to call the find method of the model to query a piece of data in the database.

public function fetchSingleData($id)
{
    $yourModel = new YourModelName;
    $data = $yourModel->find($id);
    return view('singledata', compact('data','id'));
}
Copy after login

In the above code, we first instantiate the model and then call the find method, passing a parameter $id (this will query the database for records with the given ID). Then pass the data to the view. After putting the data into the view, you can use it to render HTML.

In a view, you can use the following code to render a single field value of query data.

{{ $data->fieldname }}
Copy after login

This will output the value of the "fieldname" field queried in the database.

Conclusion

In Laravel, querying the database is a very basic task. Using models, querying the database is like ordinary API calls in an object-oriented programming language. In this article, we learned how to query a database in Laravel, laying the foundation for developing Laravel web applications. Now, you can continue to delve into Laravel’s other advanced features with database queries.

The above is the detailed content of How to query a database 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