将图像保存到数据库

时间:2019-08-13 07:05:43

标签: laravel

我想将映像名称保存到数据库,但始终将其保存到C:\ xampp \ tmp \ phpAB3A.tmp。顺便说一句我不使用xampp,我使用laragon。如何更改路径?我想保存到存储/应用/公共

        'name'=> 'required',
        'email' => 'required',
        //'logo' => 'required',


        $imageName = time().'.'.request()->logo->getClientOriginalExtension();
        request()->logo->move(storage_path('app/public'), $imageName);

        Company::create($request->all());
        return redirect()->route('company.dashboard')->with('Success');

3 个答案:

答案 0 :(得分:0)

在您的Company :: create中,您必须定义路径...类似Company::create([ 'path' => torage_path('app/public',$imageName),....]);

答案 1 :(得分:0)

$input = $request->all();
$fileName = '';
if ($request->hasFile('logo')) {
    $destinationPath = storage_path('app/public');
    $file = $request->logo;
    $fileName = time() . '.'.$file->clientExtension();
    $file->move($destinationPath, $fileName);
}
$input['logo'] = $fileName;
Company::create($input);

答案 2 :(得分:0)

我正在使用laravel 5.8,此代码对我有用。

在控制器中

<?php

public function store()
{
    // I did like this, because storeLogo method is reusable
    $data = request()->validate([ /* ... */ ]);
    $company = Company::create($data);
    $this->storeLogo($company);

    // but you can do like this
    $data = request()->validate([ /* ... */ ]);
    if (request()->has('logo')) {
        $data['logo'] = request()->logo->store('', 'public');
    }
    $company = Company::create($data);
}

private function storeLogo(Company $company)
{
    if (request()->has('logo')) {
        $company->update([
            'logo' => request()->logo->store('', 'public'),
            // logo file is stored at /storage/app/public/
        ]);
    }
}