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
First of all as mentioned in the documentation:
Use this command if you haven’t before:
After that you are missing
resume
name you created. You have to pass it to yourcreate()
method:Then just change the URL:
Now simply just:
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 yourCareer
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 theCareer
model instance that represents the uploaded file. For example: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 thegetUrlAttribute
method is relative to the server root, not the file system path.