Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Upload File Example

Rodolfo Ruiz edited this page Dec 9, 2018 · 1 revision
  1. First check your server configuration. In this case I do not want my server to handle files bigger than 300 mb. Change this at your convenience:
    • php.ini (/etc/php/7.2/fpm/php.ini)
      • upload_max_filesize = 300M
      • post_max_size = 300M
      • memory_limit = 512M
      • file_uploads = On
      • max_execution_time = 150
    • nginx.conf (/etc/nginx/nginx.conf or site config)
      • client_max_body_size 300M
      • client_body_timeout 12;
      • client_header_timeout 12;
      • keepalive_timeout 15;
      • send_timeout 10;
    • Reload php-fpm and nginx
      • service php7.2-fpm reload
      • service nginx reload
  2. Upload Controller Example:
<?php

namespace App\Http\Controllers\Admin;

use App\Models\Category;
use App\Traits\Controllers\ResourceController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoriesController extends Controller
{
    use ResourceController;

    /**
     * @var string
     */
    protected $resourceAlias = 'admin.categories';

    /**
     * @var string
     */
    protected $resourceRoutesAlias = 'admin::categories';

    /**
     * Fully qualified class name
     *
     * @var string
     */
    protected $resourceModel = Category::class;

    /**
     * @var string
     */
    protected $resourceTitle = 'Categories';

     /**
     * Used to validate store.
     *
     * @return array
     */
    private function resourceStoreValidationData()
    {
        return [
            'rules' => [
                'name' => 'required|min:3|max:255'
                'url' => 'required|url',
                'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
                'image_path' => 'required', // Validating Successful Uploads
            ],
            'messages' => [],
            'attributes' => [],
        ];
    }

    /**
     * Used to validate update.
     *
     * @param $record
     * @return array
     */
    private function resourceUpdateValidationData($record)
    {
        return $this->resourceStoreValidationData();
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param null $record
     * @return array
     */
    private function getValuesToSave(Request $request, $record = null)
    {
        $values = $request->only([
            'name', 'url', 'image'
        ]);

        // Storing Uploaded File. Validating Successful Uploads
        if ($request->file('image')->isValid()) {
                // @see https://laravel.com/docs/5.7/requests#storing-uploaded-files
               $values['image_path'] = $request->photo->store('image');
        }

        return $values;
    }

     /**
     * @param array $data
     * @return array
     */
    private function filterCreateViewData($data = [])
    {
        // Add additional data
        $data['addVarsForView']['formFiles'] = true;

        return $data;
    }

     /**
     * @param $record
     * @param array $data
     * @return array
     */
    private function filterEditViewData($record, $data = [])
    {
        // Add additional data
        $data['addVarsForView']['formFiles'] = true;

        return $data;
    }
}
Clone this wiki locally