rosiejaneenomar
rosiejaneenomar

Reputation: 1012

Join table in laravel - Pagination not working

//get all the data of both table

foreach($tables as $table){

 $table_history = $table.'_history';
 $result[] = DB::table($table)->join($table_history, $table.user_id, '=',$table_history.user_id )->paginate(5);

}

When I tried

<?php echo $result->links(); ?>

It doesn't return anything.

And also trying to print_r the result

<?php print_r($result); ?>

It returned:

Illuminate\Pagination\Paginator Object ( [factory:protected] => Illuminate\Pagination\Factory Object ( [request:protected] => Illuminate\Http\Request Object ( [json:protected] => [sessionStore:protected] => [attributes] => Symfony\Component\HttpFoundation\ParameterBag Object ( [parameters:protected] => Array ( ) ) [request] => Symfony\Component\HttpFoundation\ParameterBag Object ( [parameters:protected] => Array ( ) ) [query] => Symfony\Component\HttpFoundation\ParameterBag Object ( [parameters:protected] => Array ( ) ) [server] => Symfony\Component\HttpFoundation\ServerBag Object ( [parameters:protected] => Array ( [DOCUMENT_ROOT] => C:\xampp\htdocs\system101\public [REMOTE_ADDR] => ::1 [REMOTE_PORT] => 53241 [SERVER_SOFTWARE] => PHP 5.4.7 Development Server [SERVER_PROTOCOL] 

I tried making the variable into an array:

$result[] = DB::table('user')->join('position', user.user_id, '=',position.user_id )->get();

It displays all the data needed. But still, I did change it to ->paginate(5) It returned like the one above.

How can I display the paginated links? Or is there any other way how to do pagination?

Upvotes: 0

Views: 2713

Answers (1)

Rafael
Rafael

Reputation: 7746

Join takes only string parameters

Laravel join syntax

join(string $table, string $one, string $operator = null, string $two = null, string $type = 'inner', bool $where = false)

To fix make your db attributes strings in your query

$result = DB::table('user')
             ->join('position', 'user.user_id', '=', 'position.user_id')->paginate(5);

Moreover...

foreach($tables as $table):

    $table_history = $table.'_history';

    $result = DB::table($table)
                    ->join("$table_history", "$table.user_id", '=', "$table_history.user_id")
                    ->paginate(5);

endforeach;

Upvotes: 1

Related Questions