PHP开发框架Laravel数据库操作方法总结,框架laravel
PHP开发框架Laravel数据库操作方法总结,框架laravel
一、读/写连接
有时您可能希望使用一个SELECT语句的数据库连接,,另一个用于插入、更新和删除语句。Laravel使这微风,将始终使用正确的连接是否使用原始查询,查询生成器或雄辩的ORM。
如何读/写连接应该配置,让我们看看这个例子:
复制代码 代码如下:
'mysql' => array('read' => array('host' => '192.168.1.1'),'write' => array('host' => '196.168.1.2'),'driver' => 'mysql','database' =>'database','username' => 'root','password' => '','charset' => 'utf8','collation' => 'utf8_unicode_ci','prefix' => '')
注意,两个键添加到配置阵列:读和写。这两个键有数组值包含一个关键:主机。其余的读写数据库选项从主mysql连接将合并后的数组。所以,我们只需要将物品放入读取和写入数组如果我们希望覆盖主要数组中的值。所以,在这种情况下,192.168.1.1将被用作“读”连接,while192.168.1.2将被用作“写”连接。数据库凭证、前缀、字符集和所有其他选项在主mysql数组将跨两个共享连接。
二、运行查询
一旦你已经配置了数据库连接,你可以使用DB运行查询类。
运行一个Select查询
复制代码 代码如下:
$results = DB::select('select * from users where id = ?', array(1));
结果的选择方法总是返回一个数组。
运行一个Insert语句
复制代码 代码如下:
DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));
运行一个更新语句
复制代码 代码如下:
DB::update('update users set votes = 100 where name = ?', array('John'));
运行一个Delete语句
复制代码 代码如下:
DB::delete('delete from users');
注意:update和delete语句返回的行数的影响操作。
运行一个通用声明
复制代码 代码如下:
DB::statement('drop table users');
查询事件监听
你可以查询事件监听使用DB::听方法:
复制代码 代码如下:
DB::listen(function($sql, $bindings, $time){ //});
三、数据库事务
运行在一个数据库事务的一组操作,您可以使用事务方法:
复制代码 代码如下:
DB::transaction(function(){ DB::table('users')->update(array('votes'
=> 1)); DB::table('posts')->delete();});
注意:在事务抛出的任何异常关闭将导致自动事务将回滚
有时你可能需要开始一个事务:
复制代码 代码如下:
DB::beginTransaction();
你可以通过回滚事务回滚方法:
复制代码 代码如下:
DB::rollback();
最后,您可以通过提交方法:提交一个事务
复制代码 代码如下:
DB::commit();
四、访问连接
当使用多个连接,你可以访问它们通过DB::连接方法:
复制代码 代码如下:
$users = DB::connection('foo')->select(...);
你也可以访问原始的、潜在的PDO实例:
复制代码 代码如下:
$pdo = DB::connection()->getPdo();
有时你可能需要重新连接到一个给定的数据库:
复制代码 代码如下:
DB::reconnect('foo');
如果你需要断开从给定的数据库将超过底层PDO实例'smax_connections限制,使用断开连接方法:
复制代码 代码如下:
DB::disconnect('foo');
五、查询日志
默认情况下,Laravel日志保存在内存的所有查询运行当前的请求。然而,在某些情况下,例如当插入的行数,这可能会导致应用程序使用多余的内存。禁用日志,你可以使用disableQueryLog方法:
复制代码 代码如下:
DB::connection()->disableQueryLog();
o得到一组执行的查询,您可以使用getQueryLog方法:
复制代码 代码如下:
$queries = DB::getQueryLog();
280907494 开发群,群里很多搞这个的。
你去下个dedecms

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.
