首页 文章

增加laravel 5.6中的count值

提问于
浏览
0

我有两个表rta_list和rta_reg_company . 我在rta_list表中有一个列数 .

Schema::create('rta_list', function (Blueprint $table) {
    $table->increments('rta_id');
    $table->string('rta_name');
    $table->string('rta_address');
    $table->string('rta_phone');
    $table->string('rta_email')->unique();
    $table->integer('count'); 
    $table->timestamps();
});

 Schema::create('rta_reg_company', function (Blueprint $table) {
    $table->increments('company_id');
    $table->integer('rta_id')->unsigned();
    $table->foreign('rta_id')
        ->references('id')
        ->on('rta_lists')
        ->onDelete('cascade');
    $table->string('company_name');
    $table->string('company_isin');
    $table->string('company_script');
    $table->string('company_address');
    $table->string('company_phone');
    $table->string('company_email');
    $table->timestamps();
});

当我添加新的RTA注册公司时,我想通过rta_id增加rta_list表的计数值 . 例如:如果我添加一个公司,那么必须在rta_reg_company表中添加该值,并且在rta_list表的count列中count必须为1 .

还有如何通过视图中的rta_id显示计数值..帮助需要.....

1 回答

  • 1

    您可以使用laravel Observer在模型创建时引发事件 . 然后您可以在模型Observer上更新如下:

    public function created(Company $company)
        {
            DB::table('rta_list')->increment('count');
        }
    

相关问题