Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Override name in variable

I have a function

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;
    }

Next I call this function

$file = $article->saveImage($request, 'image_detail', '/storage/article/' .$article->id. '/');

The question now is, I have a $requestField, which now has the value 'image_detail'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

it should have this meaning everywhere, except for these lines

$pathFull. $requestField. '-'. $uploadname
$path. $requestField. '-'. $uploadname

I want the field $requestField to be converted to such a value 'image-detail', that is, to replace the underscore '_' with a dash '-', is it possible to do this at all in this function only for separate lines?

>Solution :

The Str::replace method replaces a given string within the string:

use Illuminate\Support\Str; 

$your_variable = 'image-detail';

$replaced = Str::replace('-', '_', $your_variable);

// image_detail
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading