Laravel - 防止在数据库

时间:2015-12-09 02:54:33

标签: laravel laravel-5.1

我有一个表格,我保留将在网站上使用的所有设置,它们保存在缓存中,我正在尝试上传favicon,但是在上传图像时,favicon行会更新并且为空临时路径的键值是同时创建的,我该如何解决?

enter image description here 您可以在图像中看到空白字段...

路线

Route::put('/', ['as' => 'setting.update', 'uses' => 'Admin\AdminConfiguracoesController@update']);

模型

class Setting extends Model
{
    protected $table = 'settings';

    public $timestamps = false;

    protected $fillable = ['value'];
}

控制器

class AdminConfiguracoesController extends AdminBaseController
{
    private $repository;

    public function __construct(SettingRepository $repository){
        parent::__construct();
        $this->repository = $repository;
    }

    public function update(Request $request, Factory $cache)
    {
        $settings = $request->except('_method', '_token');

        $this->repository->update($settings);

        $cache->forget('settings');

        return redirect()->back();
    }
}

存储库

class SettingRepository{
    private $settings;

    public function __construct(Setting $settings)
    {
        $this->settings = $settings;
    }

    public function update($key, $value = null)
    {
        if (is_array($key))
        {
            foreach ($key as $name => $value)
            {
                if( $name == "website_favicon" ){
                    $imageName = $key['website_favicon']->getClientOriginalName();

                    $this->update($name, asset('public/images/website/'.$imageName));

                    $key['website_favicon']->move(
                        base_path() . '/public/images/website/', $imageName
                    );
                } else{
                    $this->update($name, $value);
                }
            }
        }

        $setting = $this->settings->firstOrCreate(['name' => $key]);
        $setting->value = $value;
        $setting->save();
    }

    public function lists()
    {
        return $this->settings->lists('value', 'name')->all();
    }
}

1 个答案:

答案 0 :(得分:1)

问题是在存储库中的foreach循环之后缺少return语句。循环后的代码将被执行。 $key是一个数组,$value是上传文件的临时值,它将在循环内设置。

正如我在评论中提到的,您不应该使用存储库来上传文件。改为在控制器中执行:

AdminConfiguracoesController.php

class AdminConfiguracoesController extends AdminBaseController
{
    private $repository;

    public function __construct(SettingRepository $repository)
    {
        parent::__construct();
        $this->repository = $repository;
    }

    public function update(Request $request, Factory $cache)
    {
        $settings = $request->except('_method', '_token', 'website_favicon');

        if ($request->hasFile('website_favicon'))
        {
            $this->uploadImage($request->file('website_favicon'), 'website_favicon');
            $cache->forget('website_favicon');
        }

        $this->repository->update($settings);

        $cache->forget('settings');

        return redirect()->back();
    }

    private function uploadImage(UploadedFile $image, $key)
    {
        $image->move(public_path('images/website'), $image->getClientOriginalName());

        $this->repository->update($key, $image->getClientOriginalName());
    }
}

SettingRepository.php

class SettingRepository
{

    private $settings;

    public function __construct(Setting $settings)
    {
        $this->settings = $settings;
    }

    public function update($key, $value = null)
    {
        if (is_array($key))
        {
            foreach ($key as $name => $value)
            {
                $this->update($name, $value);
            }

            return; // This was missing!
        }

        $setting = $this->settings->firstOrCreate(['name' => $key]);
        $setting->value = $value;
        $setting->save();
    }

    public function lists()
    {
        return $this->settings->lists('value', 'name')->all();
    }
}

您可以进一步重构这一点,以使用上传图像的作业,但现在这样就太过分了。