G
G
Good Samaritan2019-12-24 14:34:37
Laravel
Good Samaritan, 2019-12-24 14:34:37

How to get the following records in ajax request?

I'm trying to do ajax upload of articles.

public function index(Request $request) {
        $tasks = Task::paginate(5);

        if($request->ajax()){
            $tasks = Task::take(5)->offset(5)->limit(5)->get();

            $response="";
            foreach ($tasks as $task){
                $response.="
                  <div class='card'>
                      <div class='card-content'>
                          <span class='card-title activator grey-text text-darken-4'>$task->title<i class='material-icons right'>more_vert</i></span>
                            <p><a href=\"#\">This is a link</a></p>
                      </div>
                      <div class='card-reveal'>
                          <span class='card-title grey-text text-darken-4'>$task->title<i class='material-icons right'>close</i></span>
                          <p>$task->text</p>
                      </div>
                   </div>";
            }
            return $response;

        }else {
            return view('home', ['tasks' => $tasks]);
        }
    }

view
@extends('layouts.app')
@section('title', 'Master')

@section('content')
    <div id="tasks">
    @foreach ($tasks as $task)
        <div class="card">
            <div class="card-content">
                <span class="card-title activator grey-text text-darken-4">{{$task->title}}<i class="material-icons right">more_vert</i></span>
                <p><a href="#">This is a link</a></p>
            </div>
            <div class="card-reveal">
                <span class="card-title grey-text text-darken-4">{{$task->title}}<i class="material-icons right">close</i></span>
                <p>{{$task->text}}</p>
            </div>
        </div>
    @endforeach
    </div>
    <center><a class="waves-effect waves-light btn" id="load-more">Load more</a></center>
@endsection

js
$( document ).ready(function() {
    $("#load-more").click(function () {
        console.log("load more");
        $.get(
            "/tasks",
            {
                param1: "param1",
            },
            onAjaxSuccess
        );

        function onAjaxSuccess(data)
        {
            console.log(data);
        }
    })
});

How to increase the offset when clicking on load-more in $tasks = Task::take(5)->offset(5)->limit(5)->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Good Samaritan, 2019-12-24
@djamali

$("#load-more").click(function () {
offset+=20;
controller:
$tasks = Task::take(20)->offset($request->query('offset'))->limit (20)->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question