thunder-birds-studio/laravel-helpers

laravel 框架的辅助工具

v1.1.0 2018-07-04 16:04 UTC

This package is not auto-updated.

Last update: 2024-09-20 07:52:47 UTC


README

laravel 框架的辅助工具。

1. 辅助函数 fof(Class::class, $class_or_id) 用于自动通过 idModel 查找模型。

这可能在某些情况下有所帮助。当使用设计模式,并且每次向 ServicesRepositories 传递数据时不需要进行查询时,或者当你不确定应该将 Model 还是仅 id 传递给其他类时。

class FooController
{
    /**
     * @var FooService
     */
    protected $foo_service;
    
    /**
     * @param FooService $foo_service
     */
    public function __construct(FooService $foo_service)
    {
        $this->foo_service = $foo_service;
    }
    
    /**
     * @param int $foo_id
     */
    public function bar($foo_id)
    {
        $this->foo_service->bar($foo_id);
    }
    
    /**
     * @param FooModel $foo
     */
    public function barz(FooModel $foo)
    {
        $this->foo_service->bar($foo);
    }
}
class FooService
{
    /**
     * @param FooModel|int $foo
     */
    public function bar($foo)
    {
        $foo = fof(FooModel::class, $foo);
        
        // Do somethings.
    }
}