Javascript IIFE Template


var NinjaLibrary = (function () {
    var aPrivateFunction = function(value) {
        return value * 2;

	var doSomething = function (value) {
        return aPrivateFunction(value);
    //publicly accessible functions:
    return {
        doSomething: doSomething,





Laravel 5 – Validate unique field on edit

When you need to validate a unique field on edit, you need to give Laravel an id of the model you are editing, otherwise it will complain that the existing record exists, which is what it should do when creating a new record, but when updating a record you don’t want this to happen. So you could do for example:

var $method = $request->method();
$invoiceId = $request->invoice_id;

 if($method == 'POST') {
    return [
                        'subject' => 'required|max:255',
                        'invoice_number' => 'required|max:255|unique:invoices',
                        'issue_date' => 'required|date',
  } else if($method == "PATCH")   {
                    return [
                        'subject' => 'required|max:255',
                        'invoice_number' => 'required|max:255|unique:invoices,id,' . $invoiceId, 
                        'issue_date' => 'required|date',


The important part is 1unique:invoices,id,'.$invoiceId; You want a unique value from the invoices table, using the id field, and you want to ignore validation for $invoiceId.

Laravel 5 – Upload an image into the public folder

The following is an example of how to upload an image into the public folder of Laravel. In this case we upload an image into public/img. The Intervention Image plugin is used to create and resize the image to new dimensions.

 $file = Input::file('logo');
            if ($file) {
                $input['logo'] = "";
                if ($file) {

                    $destinationPath = 'uploads/img';
                    $filename = str_random(6) . "_" . $file->getClientOriginalName();
                    $uploadSuccess = $file->move($destinationPath, $filename);
                    $input['logo'] = $destinationPath . "/" . $filename;
                    //open an image file with Intervention Image  
                    $img = Image::make($input['logo']);

                    //save the original image

                    // keep image width under 600px while maintaining aspect ratio
                    if ($img->width() > 600) {
                        $img->resize(600, null, function ($constraint) {
                    // keep image height under 600px while maintaining aspect ratio
                    if ($img->height() > 600) {
                        $img->resize(null, 600, function ($constraint) {
                    $img->save($input['logo']); // saved the resized version of the image


On the HTML page you would simply have a normal file field with name="logo" if following the example.

In production code you would also need to validate that the file is actually an image, and may take into account the image size. With Laravel’s validation system you could do something along the lines of the following:

            $rules = array(
                 'file' => 'image|max:3000',
             $validation = Validator::make($input, $rules);
            if ($validation->fails()) {
                //handle failure 

In the above code we assume that the file is put of the $input array.

Laravel 5 – Upload multiple files to Laravel private storage

Very often you will want 2 things:

  • Upload files not into the public folder, but into a private folder. This is what Laravel storage is for.
  • Upload multiple files at once.

You can achieve it with the following controller code:

  public function add()
        try {

            $file =Input::file('file');
            $extension = $file->getClientOriginalExtension();
            Storage::disk('local')->put($file->getFilename() . '.' . $extension, File::get($file));
            $entry = new Fileentry();
            $entry->mime = $file->getClientMimeType();
            $entry->original_filename = $file->getClientOriginalName();
            $entry->filename = $file->getFilename() . '.' . $extension;

           //Do redirects, responses, etc.

        }catch(Exception $e) {
                //handle errors

The HTML would be as simple as a normal HTML5 multiple file upload field, like the following:

 <form action="files/add.php">
  Select images: <input type="file" name="file" multiple>
  <input type="submit">

If you want to use drag and drop uploaders, I’d recommend you can use something like Dropzone.js. But don’t do the enhancements until you have the basic upload field working, then you can incrementally make things nicer. You should not need to change much if any controller code to deal use Dropzone.js.