Search code examples
phplaravelfiledirectorylaravelcollective

How can I add media files in laravel?


A user can register/sign up as either a Job Seeker or an Employer. If he is a Job Seeker, he can create a profile on the create profile page. On the form their are 4 inputs with the file type, among other inputs. See screenshot below. enter image description here

I have a Table called job_seeker_profiles, there are several columns in there, but only these 4 are relevant to my issue. resume_id, video_one_id, video_two_id, video_three_id

I also have 2 Tables, Resumes and Videos, each have one column called 'file'. This is working, the file paths are being inserted into the database.

In my public folder, I created 2 directories resumes and videos. This is where I want to store the files. This part is also working, when I upload a resume only (No Videos, they are optional) and click Create Profile, The file appears in the /resumes folder within my public folder.

The problem is, in the resume_id column in the job_seeker_profiles Table, I get this value inserted into my database "/Applications/XAMPP/xamppfiles/temp/phpx6Dmr" instead of the id of the file from the Resumes Table.

I think maybe it's a problem with my relationships?

Here is my code.

AdminJobSeekerProfilecontroller.php file:

public function store(JobSeekerCreateRequest $request)
    {
        if($file = $request->file('resume_id')) {

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

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

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

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

        }

        $input = $request->all();

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

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

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

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

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

        }

        $input2 = $request->all();

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

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

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

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

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

        }

        $input3 = $request->all();

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

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

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

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

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

        }

        $input4 = $request->all();

        JobSeekerProfile::create($input, $input2, $input3, $input4);

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

Resume.php Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Resume extends Model
{

    protected $uploads = '/resumes/';

    protected $fillable = ['file'];

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

        return $this->uploads . $resumes;

    }

}

JobSeekerProfile.php Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class JobSeekerProfile extends Model
{

    protected $dates = ['date_of_birth'];

    protected $fillable = [

        'user_id',
        'photo_id',
        'resume_id',
        'video_one_id',
        'video_two_id',
        'video_three_id',
        'first_name',
        'last_name',
        'email',
        'full_or_part_time',
        'additional_skills',
        'file'

    ];

    public function user(){

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

    }

    public function resume(){

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

    }

    public function video(){

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

    }

}

On a side note, I know I should store the video files on something like amazon s3, but I want to get it working this way for now. And if you have any recommendations for Laravel and hosting files on a cloud based system that would be great as well.


Solution

  • Thank you Tpojka. This worked for me now:

    $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;
    
            }
    
            $user->jobseekerprofile()->create($input);
    
            return redirect('/admin/job-seeker/profile/create');