Answer the question
In order to leave comments, you need to log in
Images are not loaded in Laravel via ajax - where am I wrong?
Loading consists of a route and a controller.
With the route, everything is simple, but here is the controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UploadController extends Controller
{
public function upload (Request $request) {
$path = public_path('uploads/images/');
foreach ($request->file() as $file) {
foreach ($file as $f) {
$f->move($path, str_random(5).'_'.$f->getClientOriginalName());
}
}
return 'Успех '.$path;
}
}
Answer the question
In order to leave comments, you need to log in
Let's start with the fact that you didn't specify a file name, and Laravel has a system for working with files .
It is also not known what you have on the frontend, whether the form is configured correctly and passing csrf for ajax requests
should work like this:
//photo - имя поля
if ($request->hasFile('photo')) {
$files = request()->file('photo');
foreach ($files as $file) {
//вернет имя файла, как он будет сохранен.
$name = $file->store('photo','public')]);
}
}
/storage/app/public/photo
(see the first parameter $file->store, this is the folder where, the second type of storage, for everyone or another), to throw it into public for outside access, there is a command php artisan storage:link
- create a symlink to this folder in the public directory. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question