使用经过验证的数据上传文件

时间:2018-08-24 16:55:28

标签: php laravel

在Laravel中,我有一个简单的函数,可将作业列表存储在数据库中。

最初看起来像这样:

/**
 * Store a new instance of a job posting in the database
 *
 * @param Request $request
 * @return void
 */
public function store(StoreJobPost $request)
{
    $job = new JobPost();

    $job->position = $request->get('position');
    $job->description = $request->get('description');
    $job->location = $request->get('location');
    $job->business_area = $request->get('business_area');
    $job->contract_type = $request->get('contract_type');
    $job->hours = $request->get('hours');
    $job->salary = $request->get('salary');
    $job->salary_period = $request->get('salary_period');
    $job->attached_description = $request->get('attached_description');
    $job->scheme = $request->get('scheme');
    $job->contact_name = $request->get('contact_name');
    $job->contact_email = $request->get('contact_email');
    $job->contact_number = $request->get('contact_number');
    $job->status = $request->get('status');
    $job->expires_on = $request->get('expires_on');

    $job->save();

    return redirect()->route('vacancies.index')->with('success', 'Job listing created successfully');
}

我将其缩短为这样:

/**
 * Store a new instance of a job posting in the database
 *
 * @param Request $request
 * @return void
 */
public function store(StoreJobPost $request)
{
    $validFormData = $request->validated();

    $job = JobPost::create($validFormData);

    return redirect()->route('vacancies.index')->with('success', 'Job listing created successfully');
}

但是,我还想存储一个文件。以前我做过这样的事情:

$file = $request->file('file');
$path = $file->storeAs('library/', $file->getClientOriginalName());

在那一点上,我已经:

$object->path = $path;
$object->save();

在当前情况下,是否可以从$validatedFormData取回文件,以便我可以将上载路径与其余对象一起存储在数据库中?

1 个答案:

答案 0 :(得分:3)

是的。这样做:

>= 6