Search code examples
mysqllaravel-5inner-join

Laravel 5.3 Trigger


is there a way to make a laravel trigger for this sql command? sorry I am new to laravel and having a hard time figuring it out.

So what should happen is when the table.a get its content, it will automactically fill the column with ids from the other table. is it possible for laravel? Thank you so much.

 UPDATE table_a
 INNER JOIN table.b ON table_a.account_code = 
 table.ac_code
 SET table_a.ut_id = table.ut_id, table_a.pj_id 
 = table.pj_id

Solution

  • I used to use laravel event for trigger stuff in laravel (it assumes you have models on this). In your to be listened Model_B (table_b), you can define something like :

    public function boot()
        {
        parent::boot();
        static::created(function ($model) {
                /*update table a here*/
                $ut_id = $model->ut_id;
                $pj_id = $model->pj_id;
                Table_A::customUpdate($ut_id, $pj_id);
            });
        }
    

    Please define your public customUpdate( ) as you want. Check laravel doc above to see other possibilities for boot methods: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored