gpt4 book ai didi

php - 使用 Laravel 从表单将文件保存到 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 17:49:05 25 4
gpt4 key购买 nike

大家好,我使用 Laravel 还不到 1 周。我正在尝试将注册简历页面保存到 MySQL 数据库。虽然我的表单包含一个上传文件。将其保存到数据库时,仅保存文件名,文件本身不存在。我的代码是这样的。注册 Controller .php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\candidate;

use App\Http\Requests;

class RegisterController extends Controller
{
public function register(Request $request) {
$this->validate($request, [
'surname' => 'required',
'other_name' => 'required',
'email' => 'required',
'phone' => 'required',
'gender' => 'required',
'field' => 'required',
'qualification' => 'required',
'resume' => 'required'
]);

$candidates = new Candidate;
$candidates->surname = $request->input('surname');
$candidates->other_name = $request->input('other_name');
$candidates->email = $request->input('email');
$candidates->phone = $request->input('phone');
$candidates->gender = $request->input('gender');
$candidates->field = $request->input('field');
$candidates->qualification = $request->input('qualification');

$candidates->resume = $request->input('resume');
$candidates->save();
return redirect('/career')->with('response', 'Registered Successfully');
}
}

2018_03_28_152114_create_candidates_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCandidatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('candidates', function (Blueprint $table) {
$table->increments('id');
$table->string('surname');
$table->string('other_name');
$table->string('email');
$table->string('phone');
$table->string('gender');
$table->string('field');
$table->string('qualification');
$table->string('resume');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('candidates');
}
}
然后是表格

<form class="form-horizontal pv-20" action="/career/test-apply" method="POST" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

.........
........


<div class="form-group">
<label class="col-sm-2" for="resume">Upload Resume</label>
<div class="col-sm-10">
<input class="form-control" type="file" name="resume" id="resume" accept="application/pdf">
</div>
</div>

<div class="form-group">
<label class="col-sm-6" for="resume"></label>
<div class="col-sm-6">
<button type="submit" class="btn btn-default btn-curve btn-animated pull-right">Submit <i class="fa fa-play"></i></button>
</div>
</div>
</form>

最佳答案

您可以将该文件保存在项目的 Public 目录中。然后,您只需将文件名保存在 MySQL 数据库中即可。

关于php - 使用 Laravel 从表单将文件保存到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49551192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com