yannis's avatar

file upload

Hi,

I'm actually having a issue with file uploads.

considering this function:

public function store(CreateTeamRequest $createTeamRequest)
{
    if ($createTeamRequest->hasFile('avatar')){
            $filename = $createTeamRequest->input("name").".avatar.".$createTeamRequest->file('avatar')->getClientOriginalExtension();
            $createTeamRequest->file('avatar')->move(public_path()."/img/teams/", $filename);
        }
        dd($createTeamRequest->all());
        $team = new Team();
        $team->create($createTeamRequest->all());
        Flash::success(Lang::get("forms.create_team_success", ["name" => $createTeamRequest->input("name")]));
        return redirect("/teams");
}

How do i replace the value of the avatar field in my request so the new filename i gave to the file will be saved in db by the model?

Thanks for your help

0 likes
6 replies
bestmomo's avatar

Try merge method :

$inputFile = ['name' =>  $filename];
$createTeamRequest->merge($inputFile);

Not sure it works because it's a file input.

yannis's avatar

name is already used on the form, i have to override 'avatar' input value, is it possible ?

bestmomo's avatar

With merge you override the input value if the key exists.

thepsion5's avatar

Why not this?

$teamData = $createTeamRequest->except('avatar') + ['avatar' => $filename];
$team->create($teamData);
yannis's avatar

yeah i can obviously use another var, but i wanted to know if there was a way in laravel to override input values at the same time.

thanks for your suggests anyway :)

sitesense's avatar

Why not @bestmomo method, but replace name with avatar?

$inputFile = ['avater' =>  $filename];
$createTeamRequest->merge($inputFile);

Please or to participate in this conversation.