php – How to upload video files in Laravel?-ThrowExceptions

Exception or error:

I’m trying to upload Resume and Video files in one of my forms.
Everything works except for the video filenames are not being inserted into my Videos table. They are in the table, just the column names in the Video table where the file names should be stored:
file_one, file_two, file_three are all null. They are appearing correctly in my public/videos folder.
The Resume is being uploaded correctly, both in public/resumes and in my database. Everything is being inserted into the job_seeker_profiles table.

AdminJobSeekerProfileController.php

public function store(JobSeekerProfileCreateRequest $request)
    {
        $input = $request->all();

        $user = Auth::user();

        if($file = $request->file('resume_id')) {

            $name = time() . $file->getClientOriginalName();

            $file->move('resumes', $name);

            $resume = Resume::create(['file'=>$name]);

            $input['resume_id'] = $resume->id;

        }

        if($file = $request->file('video_one_id')) {

            $name = time() . $file->getClientOriginalName();

            $file->move('videos', $name);

            $video = Video::create(['file_one'=>$name]);

            $input['video_one_id'] = $video->id;

        }

        if($file = $request->file('video_two_id')) {

            $name = time() . $file->getClientOriginalName();

            $file->move('videos', $name);

            $video = Video::create(['file_two'=>$name]);

            $input['video_two_id'] = $video->id;

        }

        if($file = $request->file('video_three_id')) {

            $name = time() . $file->getClientOriginalName();

            $file->move('videos', $name);

            $video = Video::create(['file_three'=>$name]);

            $input['video_three_id'] = $video->id;

        }

        $user->jobSeekerProfile()->create($input);

        Alert::success('Your Profile has been created successfully!')->autoclose(5000);

        return redirect('/admin/job-seeker/profile');
    }

create.blade with form

{!! Form::open(['method'=>'POST', 'action'=>'AdminJobSeekerProfileController@store', 'files'=>true]) !!}

            <div class="form-group">
                {!! Form::label('first_name', 'First Name:') !!}
                {!! Form::text('first_name', null, ['class'=> $errors->first('first_name') ? 'border-danger form-control' : 'form-control']) !!}
                <small class="text-danger">{{ $errors->first('first_name') }}</small>
            </div>

            <div class="form-group">
                {!! Form::label('last_name', 'Last Name:') !!}
                {!! Form::text('last_name', null, ['class'=> $errors->first('last_name') ? 'border-danger form-control' : 'form-control']) !!}
                <small class="text-danger">{{ $errors->first('last_name') }}</small>
            </div>

            <div class="form-group">
                {!! Form::label('email', 'Email:') !!}
                {!! Form::email('email', null, ['class'=> $errors->first('email') ? 'border-danger form-control' : 'form-control']) !!}
                <small class="text-danger">{{ $errors->first('email') }}</small>
            </div>

            <div class="form-group">
                {!! Form::label('date_of_birth', 'Date of birth:') !!}
                {!! Form::date('date_of_birth', null, ['class'=> $errors->first('date_of_birth') ? 'border-danger form-control' : 'form-control']) !!}
                <small class="text-danger">{{ $errors->first('date_of_birth') }}</small>
            </div>

            <div class="form-group">
                {!! Form::label('full_time', 'Full Time:') !!}
                {!! Form::radio('full_or_part_time', 'Full Time', ['class'=>'form-control']) !!}
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                {!! Form::label('part_time', 'Part Time:') !!}
                {!! Form::radio('full_or_part_time', 'Part Time', ['class'=>'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::hidden('user_id', Auth::user()->id, ['class'=>'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('resume_id', 'Attach Resume:') !!}
                {!! Form::file('resume_id', null, ['class'=> $errors->first('resume_id') ? 'border-danger form-control' : 'form-control']) !!}
                <br><small class="text-danger">{{ $errors->first('resume_id') }}</small>
            </div>

            <h4>Please upload a few short videos (30 seconds max) for a better chance of being noticed. <small>(optional)</small></h4><br>

            <div class="form-group">
                {!! Form::label('video_one_id', '1. Tell us a little about yourself:') !!}
                {!! Form::file('video_one_id', null, ['class'=>'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('video_two_id', '2. Tell us about your past work experiences:') !!}
                {!! Form::file('video_two_id', null, ['class'=>'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('video_three_id', '3. What sets you apart?:') !!}
                {!! Form::file('video_three_id', null, ['class'=>'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('experience', 'Experience:') !!}
                {!! Form::textarea('experience', null, ['class'=>'form-control', 'rows'=>'10', 'maxlength'=>'1000']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('additional_skills', 'Additional Skills (optional):') !!}
                {!! Form::textarea('additional_skills', null, ['class'=>'form-control', 'rows'=>'3', 'maxlength'=>'250']) !!}
            </div>

            <div class="form-group">
                {!! Form::submit('Create Profile', ['class'=>'btn btn-primary']) !!}
            </div>
            <br><br><br><br>

            {!! Form::close() !!}

Video.php model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    protected $uploads = '/videos/';

    protected $fillable = ['file_one, file_two, file_three'];

    //create an accessor
    public function getFileAttribute($video){

        return $this->uploads . $video;

    }

    public function jobSeekerProfile(){

        return $this->belongsTo('App\JobSeekerProfile');

    }

}
How to solve:

Change this into proper array values:

protected $fillable = ['file_one', 'file_two', 'file_three'];

It was treating it as a single string value instead of the three columns you have in your db.

Leave a Reply

Your email address will not be published. Required fields are marked *