0
votes

I want to use the Illuminate/pagination library outside of Laravel in my project. I use the Eloquent for models and in the controller I call the static paginate method to make pagination:

public function index()
{
    $tasks = Task::paginate(3);
    $this->setView("home/index", ["tasks" => $tasks] );

}

When I try to display items in view only first 3 of them are shown and there is an error in the bottom of it:

Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in E:\OSPanel\domains\MVCtodo\vendor\illuminate\pagination\AbstractPaginator.php on line 519

Fatal error: Uncaught Error: Call to a member function make() on null in E:\OSPanel\domains\MVCtodo\vendor\illuminate\pagination\LengthAwarePaginator.php:91 Stack trace: #0 E:\OSPanel\domains\MVCtodo\vendor\illuminate\pagination\LengthAwarePaginator.php(79): Illuminate\Pagination\LengthAwarePaginator->render(NULL, Array) #1 E:\OSPanel\domains\MVCtodo\app\views\home\index.php(82): Illuminate\Pagination\LengthAwarePaginator->links() #2 E:\OSPanel\domains\MVCtodo\app\core\Controller.php(13): require_once('E:\OSPanel\doma...') #3 E:\OSPanel\domains\MVCtodo\app\controllers\HomeController.php(9): Controller->setView('home/index', Array) #4 E:\OSPanel\domains\MVCtodo\app\core\Route.php(16): HomeController->index() #5 E:\OSPanel\domains\MVCtodo\app\core\page.php(8): Route->__construct()

6 E:\OSPanel\domains\MVCtodo\index.php(4): require_once('E:\OSPanel\doma...') #7 {main} thrown in

E:\OSPanel\domains\MVCtodo\vendor\illuminate\pagination\LengthAwarePaginator.php on line 91

Here is the peace of code in the view:

<table id="myTable" border="1" width="80%" class="tablesorter">
    <thead>
        <tr>
            <th>Username</th>
            <th>Email</th>
            <th>Task</th>
            <th>Accepted</th>
        </tr>
    </thead>
    <tbody>
        <?php 
            $taskNumber = 1;

            foreach($data['tasks'] as $task)
                {   $accepted = $task->accepted ? "<i class='fa fa-check-square-o fa-2x'></i>" : "";  
                    echo "<tr>
                    <td>$taskNumber. $task->username</td>
                    <td>$task->email</td>
                    <td>$task->task</td>
                    <td>$accepted</td>
                    </tr>
                    ";
                    $taskNumber++;
                }

            ?>
    </tbody>
</table>
<?php echo $data['tasks']->links(); ?>
1

1 Answers

0
votes

I Suggest that in your controller, if you know the view path, then do this,

public function index()
{
    $tasks = Task::paginate(3);
    return view("home.index", ["tasks" => $tasks] );
}

that is if the view is in the home folder, and its called index. then in the view its self,

<table id="myTable" border="1" width="80%" class="tablesorter">
                    <thead>
                        <tr>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Task</th>
                            <th>Accepted</th>
                        </tr>
                    </thead>
                    <tbody>
                            @foreach($tasks as $task)
                              $accepted = $task->accepted ? "<i class='fa fa-check-square-o fa-2x'></i>" : ""; 
                              <tr>
                                    <td>$taskNumber. $task->username</td>
                                    <td>$task->email</td>
                                    <td>$task->task</td>
                                    <td>$accepted</td>
                              </tr>;
                          @endforeach
                    </tbody>
                </table>
                {{ $tasks->links()}}