zxywvu's avatar

How to redirect()

I want to redirect to the home page after uploading multiple images.

Controller

public function store(Request $request)
{
    $path = null;
    if ($request->hasFile('file'))
    {
        $file = $request->file('file');
        $name = rand(16000,17000).time();
        $extension = $file->getClientOriginalExtension();
        $fileName = $name . '.' . $extension;
        $path = $file->storeAs($this->path, $fileName, 'public');
    }
    $gallery = Media::query()->create([
        'image' => $path,
    ]);
    return response()->json($gallery);
	return redirect()->route('home');
}
0 likes
8 replies
Nakov's avatar

You can't have two redirects.. You either return the result which is this line return response()->json($gallery); or redirect to the homepage return redirect()->route('home'); and then fetch the images from your database there.

Sinnbeck's avatar

Is this an ajax request? If it is, you need to redirect in Javascript

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@azadi then in the succes callback you do the redirect

window.location = '/' 
1 like
Sinnbeck's avatar

@azadi I don't know what that means in this context ? Maybe show the Javascript code

Nihir's avatar
public function store(Request $request)
{
    $path = null;
    if ($request->hasFile('file'))
    {
        $file = $request->file('file');
        $name = rand(16000,17000).time();
        $extension = $file->getClientOriginalExtension();
        $fileName = $name . '.' . $extension;
        $path = $file->storeAs($this->path, $fileName, 'public');
    }
    $gallery = Media::query()->create([
        'image' => $path,
    ]);
 	return redirect()->route('home');
	//Or you can directly call a particular URL:

	//return redirect('/');
}

Please or to participate in this conversation.