Table of Contents
PHP开发框架Laravel数据库操作方法总结,框架laravel
php_laravel框架
谁有php的源码框架(可以读取数据库,与后台添加数据)
Home php教程 php手册 PHP开发框架Laravel数据库操作方法总结,框架laravel

PHP开发框架Laravel数据库操作方法总结,框架laravel

Jun 13, 2016 am 09:25 AM
laravel php Database operations

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();

php_laravel框架

280907494 开发群,群里很多搞这个的。
 

谁有php的源码框架(可以读取数据库,与后台添加数据)

你去下个dedecms
 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Laravel's geospatial: Optimization of interactive maps and large amounts of data Laravel's geospatial: Optimization of interactive maps and large amounts of data Apr 08, 2025 pm 12:24 PM

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

Laravel and the Backend: Powering Web Application Logic Laravel and the Backend: Powering Web Application Logic Apr 11, 2025 am 11:29 AM

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles