0

Did is what I did so far

  1. npm i flatpickr --save
  2. Add @import "~flatpickr/dist/flatpickr.css"; to the app.scss
  3. index.blade.php
@section('content')
<div class="form-group">
<label for="loaned">Loaned</label>
<input type="date" class="form-control" name="loaned" id="loaned">
</div>
@endsection

@section('scripts')
    <script>
        flatpickr('#loaned')
    </script>
@endsection

I get an error, "flatpickr is not defined". I read the documentation but it, I don't know what to do with this part:

// commonjs
const flatpickr = require("flatpickr");

// es modules are recommended, if available, especially for typescript
import flatpickr from "flatpickr";

5 Answers 5

5

You can add do this in one of two ways:

In your app.js (usually under the resources folder):

import flatpckr from 'flatpickr';
window.flatpckr = flatpckr;

This will expose the flatpckr function in your global window object.

An alternative is to add all your script code in your script files instead of in the blade files e.g.:

import flatpckr from 'flatpickr';

// Maybe add conditions on when to run this
flatpckr('#loaned');

This way you don't "pollute" your window object with a bunch of libraries without needing to.

Sign up to request clarification or add additional context in comments.

Comments

0

In this case I add the following line to webpack.mix.js

mix.copy('node_modules/flatpickr/dist/flatpickr.css', 'public/css/flatpickr.css');

and import the css file the regular way.

<link rel="stylesheet" href="/css/flatpickr.css" />

Addition: of course you need to run npm run dev and if you use versioning, add the file to your git.

Comments

0

This works for me:

  1. npm i flatpickr --save
  2. Register flatpickr in the resources/js/app.js
require('./bootstrap');
require('alpinejs');
require("flatpickr");
  1. Add an import flatpickr in the resources/css/app.css
@import 'flatpickr/dist/flatpickr.css';
  1. Run the npm run dev or npm run watch.

Comments

0

Alternatively, you can try this package.
https://github.com/Laratipsofficial/laravel-flatpickr

This package is created for easier integration between Laravel and flatpickr.

Quoted from their readme: "Using this package you can add a beautiful date or datetime picker into your project without touching any javascript with the power or laravel component. It is just a laravel component wrapper for the Flatpickr javascript library."

Comments

0

Using a cdn and importing in a Laravel blade file i found works for me. I just imported like this:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css"/>
    <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
    <script>
    flatpickr("#reserved_from", {});
    

    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.