Laravel 5.x数据库触发器和可能的最佳实践 [英] Laravel 5.x Database Triggers and possible best practices

查看:569
本文介绍了Laravel 5.x数据库触发器和可能的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个帖子是排序的通知和问一个问题。
Hello all,
我正在开发一个大型系统,使触发器得到很好的使用。我们目前正在运行服务器端在Laravel 5.2与php 7使用phpmyadmin。在Laravel中没有真正有关如何通过迁移使用触发器的实用文档,并且从我发现你最主要是做它原始。

This post is to sort-of inform and ask a question. Hello all, I'm developing a large system that puts triggers to good use. We're currently running the server-side on Laravel 5.2 with php 7 using phpmyadmin. Within Laravel there isn't really much solid documentation on how to use a trigger through migrations and from what I've discovered you mostly have to do it "raw".

下面是一个简单的例子,我知道,关于如何在迁移中编写触发器:

Below is a simple example, that I know of, on how to write a trigger in a migration:

class CreateAccountTriggerTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
     public function up() 
     {   
         DB::unprepared('
             CREATE TRIGGER `my_trigger_name` AFTER INSERT ON `table_the_change_happened`
             FOR EACH ROW 

             BEGIN

             INSERT INTO `this_table`
                  (field1,field2,field3)
             VALUES
                  ('value1','value2','value3')

             END
         ');
     }

     /**
      * Reverse the migrations.
      *
      * @return void
      */
     public function down() {
          DB::unprepared('DROP TRIGGER IF EXISTS `my_trigger_name`');
     }
 }

问题:
有人知道在laravel的查询构建器中使用变量的干净的方法吗?

The Question: Does anyone know of a clean way to use variables within laravel's query builder?

方法DB :: unprepared()不允许您将绑定项附加到它。尝试在Eloquent几乎是不可能的,因为没有坚实的文档,我发现这个问题。如果任何人可以阐述这一点,我会非常感谢。提前感谢您

The method DB::unprepared() does not allow you to attach bound items to it. Trying it in Eloquent is near impossible as there is no solid documentation that I've found for this issue. If anyone can elaborate on this I would greatly appreciate it. Thank you in advance

更新
此处的每个请求都是我在使用laravel的查询构建器中的变量:

Update Per request here would be an example of what I mean by using a variable within laravel's query builder:

          $stuff = bcrypt($random_numbers);
          DB::unprepared('
             CREATE TRIGGER `my_trigger_name` AFTER INSERT ON `table_the_change_happened`
             FOR EACH ROW 

             BEGIN

             INSERT INTO `this_table`
                  (field1,field2,field3)
             VALUES
                  ('value1','value2','value3',"$stuff")

             END
         ');


推荐答案

除非我误解了你的问题,找到语句()方法,而不是 unprepared()方法。 语句()方法接受一个查询和该查询的参数绑定数组。

Unless I'm misunderstanding your question, I think you're looking for the statement() method, instead of the unprepared() method. The statement() method takes a query and an array of parameter bindings for that query.

下面的示例。值列表中的是查询参数。绑定到该参数的值将添加到在语句()方法的第二个参数中传递的数组。

Example below. The ? in the values list is the query parameter. The value to bind to that parameter is added to an array passed in the second parameter to the statement() method.

$stuff = bcrypt($random_numbers);
DB::statement('
        CREATE TRIGGER `my_trigger_name` AFTER INSERT ON `table_the_change_happened`
        FOR EACH ROW 

        BEGIN

        INSERT INTO `this_table`
            (field1,field2,field3)
        VALUES
            ('value1','value2','value3',?)

        END
    ', [$stuff]);

这篇关于Laravel 5.x数据库触发器和可能的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆