使用 Laravel 6 上传图片

Upload image with Laravel 6

我正在尝试上传图片,但是当我提交表单时什么也没有发生。我最终在数据库中得到一个空图像文件夹和一个 null 图像字段。到目前为止,我已经在网上尝试了很多可能的解决方案。

控制器

class ReportsController extends Controller
{
    /**
     * Only logged in users can access reports.
     */

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $reports = Report::with(['victim', 'lost_vehicle'])->get();
        return view('reports.index')->with('reports', $reports);
    }

    public function create()
    {
        return view('reports.create');
    }

    public function store(ReportRequest $request)
    {
        $userid = Auth::user()->id;
        request()->validate([
            // 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
        ]);
        $victim = Victim::create([
            'vic_name' => $request->input('name'),
            'vic_statment' => $request->input('statment'),
            'case_id' => uniqid(),
            'report_status' => $request->input('status'),
            'user_id' => $userid,
        ]);

        if ($files = $request->file('image')) {
            $carImage = time().'.'.$files->getClientOriginalExtension();
            $request->image->move(public_path('images'), $carImage);
            $victim->lost_vehicles()->create([
                'image' => $carImage,
            ]);
        }
        $victim->lost_vehicles()->create([
            'type' => $request->input('type'),
            'model' => $request->input('model'),
            'production_year' => $request->input('year'),
            'color' => $request->input('color'),
            'value' => $request->input('value'),
            'marks' => $request->input('marks'),
            'plate_number' => $request->input('plate'),
            'chassis_number' => $request->input('chassis'),
            'last_location' => $request->input('location'),
            'last_time_spotted' => $request->input('last_time_spotted'),
            'victim_id' => $victim->id,
        ]);

        return dd($victim);
    }
}

我将表单设置为接受文件{!! Form::open(['url'=>'reports'], array('files'=> true)) !!}

您的这部分代码有问题:

if ($files = $request->file('image')) {
    ...
    $victim->lost_vehicles()->create([
        'image' => $carImage,
    ]);
}
$victim->lost_vehicles()->create([
    ...
]);

您正确地检查了 $request->file('image') 是否存在,但是您调用了 $victim->lost_vehicles()->create() 两次。如果 lost_vehicleshasMany() 关系,这很好,但是您最终会在数据库中得到 2 条记录,一条有图像但没有信息,另一条有所有信息但没有图像。考虑重构:

$image = null;
if ($files = $request->file('image')) {
    $carImage = time().'.'.$files->getClientOriginalExtension(); 
    $request->image->move(public_path('images'), $carImage);

    $image = $carImage;
}

$victim->lost_vehicles()->create([
    'type' => $request->input('type'),
    'model' => $request->input('model'),
    'production_year' => $request->input('year'),
    'color' => $request->input('color'),
    'value' => $request->input('value'),
    'marks' => $request->input('marks'),
    'plate_number' => $request->input('plate'),
    'chassis_number' => $request->input('chassis'),
    'last_location' => $request->input('location'),
    'last_time_spotted' => $request->input('last_time_spotted'),
    'victim_id' => $victim->id,
    'image' => $image
]);

它所做的是最初将 $image 设置为 null,然后如果您的检查通过,则将其设置为代表上传图像的字符串。然后,它包含在下面的 create() 方法中,您应该关联所有数据。

除此之外,我还没有看到上传图片的方法。我使用:

\Storage::disk("local")->putFileAs("images", $files, $carImage);
// Modified to fit your variable names

还要确保您已调用 php artisan storage:link 以正确地将 link storage/app 更改为 public