0 レビュー
1 回答
php-変数の名前をオーバーライドします
機能があります
public function saveImage(Request $request, $requestField, $path) {
if ($request->hasFile($requestField)) {
$image_path = public_path($this->{ $requestField });
if (File::exists($image_path)) {
File::delete($image_path);
}
$file = $request->file($requestField);
$uploadname = $this->getUploadName($file);
$pathFull = public_path($path);
if (!File::exists($pathFull, 0775, true)) {
File::makeDirectory($pathFull, 0775, true);
}
Image::make($file)->save($pathFull. $requestField. '-'. $uploadname);
$this->{ $requestField } = $path. $requestField. '-'. $uploadname;
return $file;
}
return false;
}
次に、この関数を呼び出します
$file = $article->saveImage($request, 'image_detail', '/storage/article/' .$article->id. '/');
問題は、$requestField
があり、値が'image_detail'
これらの行を除いて、どこでもこの意味を持つはずです
$pathFull. $requestField. '-'. $uploadname $path. $requestField. '-'. $uploadname
フィールド$requestField
をそのような値'image-detail'
に変換する、つまりアンダースコア'_'
をダッシュ'-'
に置き換えることは可能ですか?別々の行に対してのみこの関数でこれを行うには?
わからない
0
レビュー
答え :
解決:
Str :: replaceメソッドは、文字列内の指定された文字列を置き換えます。
use Illuminate\Support\Str;
$your_variable = 'image-detail';
$replaced = Str::replace('-', '_', $your_variable);
// image_detail
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。