将文件保存到mysql数据库从使用laravel的表单

时间:2018-03-29 08:20:57

标签: php mysql laravel laravel-5

美好的一天每个人,我在laravel不到1周。我试图将Register CV页面保存到MySQL数据库。虽然我的表单包含上传文件。在将其保存到数据库时,只保存文件的名称,文件本身不在那里。 我的代码因此而来。 RegisterController.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');
    }
}
&#13;
&#13;
&#13;

2018_03_28_152114_create_candidates_table.php

&#13;
&#13;
<?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');
    }
}
&#13;
&#13;
&#13; 然后是表格

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

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