Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 2 years ago.
0

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.

0

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-...

0

@TerrePorter plz add your answer here to mark as solution :) cheers

0

@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.');
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Pravinslk pravinslk Joined 10 Jan 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.