jeudi 29 mars 2018

Save a File To mysql Database From a form using laravel

Good day Everyone, I'm less than 1 week old in laravel. I'm trying to save a Register CV page to MySQL database. Although my Form contains an upload file. While saving it to the database only the name of the file is saved the file itself isnt there. My code goes thus. 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');
    }
}

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');
    }
}
then the form
<form class="form-horizontal pv-20" action="/career/test-apply" method="POST" role="form">
            <input type="hidden" name="_token" value="">

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

                <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>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire