skip to Main Content

My controller:

public function store(CareerRequest $request) 
{
    $filenameWithExt = $request->file('resume')->getClientOriginalName();
    $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
    $extension = $request->file('resume')->getClientOriginalExtension();
    $fileNameToStore = $filename.'_'.time().'.'.$extension;
    $request->file('resume')->storeAs('resume', $fileNameToStore);
    $requestData["resume"] =$fileNameToStore ;

    Career::create($request->all());
    return redirect()->back();
}

My model:

class Career extends Model 
{
    protected $table = 'career';
    protected $primaryKey = 'career_id';

    public $fillable = ['username', 'email', 'phone', 'apply', 'resume'];

    public function getUrlAttribute(): string
    {
        return asset('upload/resume/'.$this->attributes['resume']);
    }
}

CareerMail:
class CareerMail extends Mailable
{

public $data;

public function __construct($data)

{
    $this->data = $data;
}

public function build()

{
    return $this->subject('Career - ' . $this->data->subject)

        ->view('emails.career')
        ->attach(
            $this->data['resume']->getRealPath(),
            [
                'as' => $this->data['resume']->getClientOriginalName(),
                'mime' => $this->data['resume']->getClientMimeType(),
            ]
        );
}

}

I have a form, when I submit the form the details goes to dashboard. And from dashboard I want to download the resume file, which has been uploaded.

2

Answers


  1. First of all as mentioned in the documentation:

    To make these files accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

    Use this command if you haven’t before:

    php artisan storage:link
    

    After that you are missing resume name you created. You have to pass it to your create() method:

    Career::create(array_merge($request->all(), ['resume' => $fileNameToStore]));
    

    Then just change the URL:

    public function getUrlAttribute(): string
    {
        return asset('storage/resume/'.$this->resume);
    }
    

    Now simply just:

    echo $career->url;
    
    Login or Signup to reply.
  2. In your code, it looks like you are storing the uploaded file in the "resume" directory within the "upload" directory. To display the path of the file where it was uploaded, you can use the getUrlAttribute method in your Career model.

    The getUrlAttribute method returns the URL of the file by concatenating the asset function with the path to the "resume" directory and the filename.

    To display the path of the file where it was uploaded, you can call the getUrlAttribute method on the Career model instance that represents the uploaded file. For example:

    $career = Career::find($id);
    $path = $career->url;
    

    The $path variable will contain the URL of the uploaded file, which includes the path to the "resume" directory and the filename. Note that the path returned by the getUrlAttribute method is relative to the server root, not the file system path.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search