0

I am uploading images using Vue and my Laravel backend endpoint looks like this below. I am unsure of what to put in the response though since it is a foreach that iterates through each file and uploads them but I still want to return the response as JSON.

public function upload(Request $request)
{
    $files = $request->file('images');

    foreach ($files as $file) {
        Storage::put('store/assets/', file_get_contents($file->getRealPath()), ['visibility' => 'public']);
    }

    return response()->json(????);
}
1
  • Is there anything in particular that you want to do with the response? Commented Apr 29, 2020 at 18:32

1 Answer 1

2
public function upload(Request $request)
{
    $files = $request->file('images');
    $stack = [];
    foreach ($files as $file) {
        $fileName = Storage::put('store/assets/', file_get_contents($file->getRealPath()),
            ['visibility' => 'public']);
        array_push($stack, $fileName);
    }

    return response()->json($stack);
}

Or you can just do like that:

$data = ["success" => 1];

return response()->json($data);

My offer: if your query is executed succesfully $data=[ "stat"=>1, "data"=>[ "first"=>1, ... ] ] else $data=[ "stat"=>0, "error"=>"Not successfull" ]

Sign up to request clarification or add additional context in comments.

1 Comment

If I went with the approach $data = ["success" => 1]; , where would I put it and how would I catch an error to send a different response if it didn't go through successfully?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.