DDSameera's avatar

asset("js/custom.js") not working

footer.blade.php - (Its works !)

<script src="{{asset('vendor/jquery.min.js')}}"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

<!--Data Table Code-->
<script>
    $("document").ready(function(){
        var table = $('.data-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('maincat.create') }}",
            columns: [
                {data: 'DT_RowIndex', name: 'DT_RowIndex'},
                {data: 'name', name: 'name'},
                {data: 'description', name: 'description'},
                {data: 'action', name: 'action', orderable: false, searchable: false},
            ]
        });

    });

</script>   

I want to put this custom js code in to asset/js/custom.js , So i did it .

footer.blade.php (It doesn't work)

<script src="{{asset('vendor/jquery.min.js')}}"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

<!--Data Table Code inserted in another Js -->
<script src="{{asset('js/custom.js')}}"></script>

after that dataTables doesn't work. It shows This error message

https://snipboard.io/5f2uis.jpg

0 likes
9 replies
DDSameera's avatar

Please read my question again. I did same thing :(

bestmomo's avatar

Your code {{ route('maincat.create') }} wont work in js file.

tomasosho's avatar

change this

ajax: "{{ route('maincat.create') }}",

to

ajax: "/maincat/create",
DDSameera's avatar

Okay . how could I maintain my custom code through the external file .

Are there any recommended way in laravel 8 ?

bestmomo's avatar
bestmomo
Best Answer
Level 52

Your js is light, why don't keep it in view ?

1 like
tomasosho's avatar

do you want to include the external custom code to the main code/layout?

1 like
DDSameera's avatar

@bestmomo ,You are right. I created separate folder "ajax" and put my code in to datatables.blade.php

@tomasosho , After that , i include that script @include('ajax.datatables')

Now it works ! Thanks !

Please or to participate in this conversation.