PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] How to modify Request values in laravel?

 January 24, 2022     laravel-5, laravel-request, php     No comments   

Issue

I have the following code,

my question is how to modify Request values?

public function store(CategoryRequest $request)
{
    try {
        $request['slug'] = str_slug($request['name'], '_');
        if ($request->file('image')->isValid()) {
            $file = $request->file('image');
            $destinationPath = public_path('images/category_images');
            $fileName = str_random('16') . '.' . $file->getClientOriginalExtension();
            $request->image = $fileName;
            echo $request['image'];
            $file->move($destinationPath, $fileName);
            Category::create($request->all());
            return redirect('category');
        }
    } catch (FileException $exception) {
        throw $exception;
    }
}

But,

on each request the output of

echo $request['image'];

outputs some text like /tmp/phpDPTsIn


Solution

You can use the merge() method on the $request object. See: https://laravel.com/api/5.2/Illuminate/Http/Request.html#method_merge

In your code, that would look like:

public function store(CategoryRequest $request)
{
    try {
        $request['slug'] = str_slug($request['name'], '_');
        if ($request->file('image')->isValid()) {
            $file = $request->file('image');
            $destinationPath = public_path('images/category_images');
            $fileName = str_random('16') . '.' . $file->getClientOriginalExtension();
            $request->merge([ 'image' => $fileName ]);
            echo $request['image'];
            $file->move($destinationPath, $fileName);
            Category::create($request->all());
            return redirect('category');
        }
    } catch (FileException $exception) {
        throw $exception;
    }
}

In spite of the methods name, it actually replaces any values associated with the member names specified by the keys of the parameter rather than concatenating their values or anything like that.



Answered By - PapaHotelPapa
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing