2015-12-09 24 views
0

我有一個表格,我保留所有將用於網站的設置,它們保存在緩存中,我試圖上傳favicon,但是當上傳圖像的favicon行更新和一個空的鍵值與臨時路徑同時創建,我該如何解決這個問題?Laravel - 防止在數據庫中創建空密鑰

enter image description here 你可以看到圖像中的空場...

路線

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

型號

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(); 
    } 
} 
+0

嗨,夥計!您的存儲庫不應該負責上傳favicon,但無論如何...上傳後,您的服務器上存在圖標嗎? – tommy

+0

@tommy是的,我把它移動到特定的文件夾,唯一的問題是空的寄存器 –

回答

1

問題是在存儲庫中的foreach循環後缺少返回語句。循環後的代碼將被執行。 $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(); 
    } 
} 

你甚至可以進一步重構這個使用該上傳圖像的工作,但是這將是矯枉過正了。