Hi,
First of all - my English is bad, sorry for that. I hope that you will understand me.
I want to create a simple application that will have a simple form with upload field. This form will create a Report with attached .pdf file. For example, the form for creating reports will have the following fields:
- Name (text)
- Date
- File (.pdf document, upload field)
- Submit button
When some user submits this form - report will be created (stored in the database) and file will be saved in public/uploads directory.
After that, user will be redirected to a page that will display all reports in the html table. That table will have three columns:
Name | Date | Filename
- Name (that was entered on the form and saved in DB) will be a link for editing report;
- Date
- Filename will be a **link for download **
So, we will have a table reports in the database, a model Report.php and controller ReportsController.php.
Please correct me if I'm wrong:
1. I guess I will need a table in the database with information about the file? If so, lets say that the name of that table is files. Then I will also have a model File.php, but I will not need a controller FilesController.php, right?
2. What fields/attributes table files should have? Since one report can have only one file, I guess that we will have a foreign key report_id? (report has one file, one file belongs to a report)
So, I suppose that table files will have the following attributes:
id | report_id | filename
3. ReportsController.php@store will be where I save the Report in the database & store uploaded file in the public/uploads directory? I'm not sure how the method store should look like. I think I would do something like this:
public function store(Requests\ReportRequest $request)
{
$report = $this->reports->create($request->only('name', 'date'));
$file = new File([
'report_id' => $report->id,
'filename' => $request->get('filename')
])->save();
$request->file('filename')->move(base_path() . '/public/uploads/', file('filename'));
return redirect('reports')->with([
'flash_message' => 'Your report has been created'
]);
}
I have a feeling that this is not done properly, that there is a much better way, right?
4. Last question: How to form links to download files? For example, if I have a VIEW index.blade.php - I suppose that ReportsController.php@index is the place to form the links for files (each report has its own file), and pass these links together with all $reports to index.blade.php, right? BUT how would you do that?