【发布时间】:2018-03-29 16:56:07
【问题描述】:
大家好,我在 laravel 还不到 1 周大。我正在尝试将注册简历页面保存到 MySQL 数据库。虽然我的表单包含一个上传文件。将其保存到数据库时,仅保存文件名,文件本身不存在。 我的代码就是这样。 注册控制器.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>
【问题讨论】:
-
您需要将文件保存在您的公共文件夹中的文件夹中,例如在
uploads文件夹中,然后您可以将文件名或路径保存在您的数据库字段中 -
我该怎么做@MehravishTemkar
-
类似问题在这里得到答案 -> Stackoverflow, laravel.io
标签: php mysql laravel laravel-5