简体   繁体   中英

Laravel: how to use laravel blade inside javascript?

Let's say i have data coming from my controller through Javascript. So the data is paginated and i want display pagination result inside the javascript. I mean...I want to use the links() method inside javascript.

Controller

$schedules= DB::table('schedules')
                ->paginate(4);

Javascript ,

success:function(schedules)
{
    console.log(schedules);
    $('#table').append(' <ul class="pager">'+{{  $schedules>links() }}+'</ul>');
})

But unfortunately not working, any help guys?

Push html paginate results from the controller, Enter the javascript code in the blade file, maybe

//controller

$paginate = '';
$paginate .= $datas->links();

//JS code in the blade file

<script type="text/javascript">
let paginate = '<div>';
    paginate += {!! json_encode($paginate) !!}
    paginate += '</div>'
consolelog(paginate)
</script>

I think you can just convert the collection to a JSON.

Try this one:

Controller

$schedules= DB::table('schedules')
                    ->paginate(4)->toArray();

return response()->json($schedules);

Javascript Ajax

success:function(schedules)
{
    console.log(schedules);
    $('#table').append(' <ul class="pager">'+schedules.links+'</ul>');
})

But in what I see, you're using the schedules also on your blade. In that case you can have a different function called for the ajax in your controller and different route.

Hope it helps!

You have an error in your blade syntax {{ }} :

    $('#table').append(' <ul class="pager">'+{{ + $schedules>links() }}+'</ul>');

Remove the extra plus + icon before + $schedules>links() like so:

 $('#table').append(' <ul class="pager">'+{{ $schedules>links() }}+'</ul>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM