小编Ami*_*aei的帖子

如何向 Laravel 模型添加自定义事件

我有付款模式,并希望在付款确认后触发自定义事件。

我的模型代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    protected $dates = [
        'created_at', 'updated_at', 'confirmed_at',
    ];

    public function confirmed(){
        $this->setAttribute('confirmed_at', now());
        $this->setAttribute('status', 'confirmed');
    }
}
Run Code Online (Sandbox Code Playgroud)

laravel eloquent laravel-5 laravel-events

3
推荐指数
2
解决办法
4158
查看次数

是否可以取消未完成的 goroutine?

考虑一组检查工作,每个检查工作都有独立的逻辑,因此它们似乎很适合并发运行,例如:

type Work struct {
    // ...
}

// This Check could be quite time-consuming
func (w *Work) Check() bool {
    // return succeed or not

    //...
}

func CheckAll(works []*Work) {
    num := len(works)
    results := make(chan bool, num)
    for _, w := range works {
        go func(w *Work) {
            results <- w.Check()
        }(w)
    }

    for i := 0; i < num; i++ {
        if r := <-results; !r {
            ReportFailed()
            break;
        }
    }
}

func ReportFailed() {
    // …
Run Code Online (Sandbox Code Playgroud)

concurrency channel go goroutine

3
推荐指数
1
解决办法
3346
查看次数

Laravel 6/7 测试:尚未设置外观根

我正在尝试为我的 Laravel 7 应用程序编写一些测试。但我一直面临A facade root has not been set.错误。

<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\TestCase;

class AddressListenerRepositoryTest extends TestCase
{
    public function testCreate()
    {
        Config::set('x', 'address_listeners'); // Cause of "A facade root has not been set" error
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释这个错误的原因吗?

phpunit laravel laravel-testing laravel-7

1
推荐指数
1
解决办法
1215
查看次数

Golang检查文件扩展名不起作用

我想从控制台获取文件路径并检查该文件是否为 vm 文件。我写了这段代码:

func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter the path of the vm file:")
path, _ := reader.ReadString('\n')
if filepath.Ext(path) != ".vm" {
    fmt.Println("Error! file must be vm file")
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用。我刚开始学习 Go 所以如果这是一个基本错误,请接受我的道歉。

file go

-1
推荐指数
2
解决办法
6430
查看次数