Take a look at the docs for accessing file uploads, https://laravel.com/docs/4.2/requests#files or https://laravel.com/docs/5.1/requests#files
Something like,
$file = $request->file('photo');
if ($request->file('photo')->isValid()) {
// get the upload file path
$realpath = $file->getRealPath();
// ... then load the file in to a var and assign to the db array
} else {
// .. handle errors if the file upload is invalid
}
It would be better to store the files in a special folder, then access them from there instead of in the db. Make sure the file db field is a blob type.
Displaying the file from a click is a bit more cumbersome.
To download the file, you will need to pass the id or something to a route that returns the file from the db with the headers set so that the browser knows it is a file.
To display the file, depends on the type of file ... image, text, etc...
Hope that helps.
Dear TerrePorter this is my question i have edit my output also here i have to get arrays and insert how its possible? please help me im in stuck https://laracasts.com/discuss/channels/laravel/call-to-member-function-move-on-a-non-object-larvel-52
@Pravinslk :)
public function store(Request $request)
{
// validate input
$this->validate($request, $this->rules);
// set destination path
$uploadDestinationPath = base_path() . '/assets/files/';
// get input
$input = $request->all();
$charge = $request->get('charge');
$date = $request->get('date');
$job = $request->get('job_id');
$id = \Auth::id();
$current_id = DB::table('charges')->max('invoice_no');
// make sure a file has been included
if ($request->hasFile('file')) {
// this form uploads multiple files
foreach ($request->file('file') as $fileKey => $fileObject ){
// make sure each file is valid
if ($fileObject->isValid()) {
// make destination file name
$destinationFileName = $job . '_' . $id . '_' . ($current_id + 1) . '.' . $fileObject->getClientOriginalExtension();
// move the file from tmp to the destination path
$fileObject->move($uploadDestinationPath, $destinationFileName);
// save the the destination filename
$dbFilenames[$fileKey] = $uploadDestinationPath . '/' . $destinationFileName;
}
}
} else {
// no files to include
//TODO:: handle error for no files uploaded (if not already covered in the $this->validate)
// set dbFilenames to empty array
$dbFilenames[] = '';
}
// make array for database
$data = array();
foreach ($charge as $key=>$value){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$input['amount'][$key],
'category'=>$input['category'][$key],
'file'=> (isset($dbFilenames[$key])?$dbFilenames[$key]:''),
// if file with key does not exist (may not happen, if part of $this->validate), then save it or save empty
'invoice_no' => $current_id + 1,
];
}
;
// save the data
DB::table('charges')->insert($data);
// redirect
Redirect::route('charges.index')->with('message', 'Charges created.');
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community