0
我有一個表格,我保留所有將用於網站的設置,它們保存在緩存中,我試圖上傳favicon,但是當上傳圖像的favicon行更新和一個空的鍵值與臨時路徑同時創建,我該如何解決這個問題?Laravel - 防止在數據庫中創建空密鑰
路線
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();
}
}
嗨,夥計!您的存儲庫不應該負責上傳favicon,但無論如何...上傳後,您的服務器上存在圖標嗎? – tommy
@tommy是的,我把它移動到特定的文件夾,唯一的問題是空的寄存器 –