Ads Top

Custom Pagination Laravel

Custom Pagination Laravel 
<?php

namespace AppHttpControllersBrand;

use IlluminateHttpRequest;
use AppHttpControllersController;
use AppInfluencer;
use AppUser;

class InfluencerController extends Controller
{
    public function index(Request $request)
    {
    $influencers = User::whereHas('influencers', function ($influencer) use ($request) {
    if ($request->exists('aud_age')) {
    $influencer->audienceAge($request->input('aud_age'));
    }
    if ($request->exists('aud_gender')) {
    $influencer->audienceGender($request->input('aud_gender'));
    }
    if ($request->exists('aud_location')) {
    $influencer->audienceLocation($request->input('aud_location'));
    }
    });

    if ($request->exists('name')) {
    $influencers->name($request->input('name'));
    }

    $fetch_data = $influencers->paginate(10);

    $in_data = array();

    foreach ($fetch_data as $row) {
    $data = array(
    'id' => $row->id,
    'phone' => $row->phone,
    'username' => $row->username,
    'full_name' => $row->first_name .' '.$row->last_name,
    'birthday' => $row->birthday,
    'photo_url' => $row->photo_url,
    'gender' => $row->gender
    );

    array_push($in_data, $data);
    }

    $page = $request->exists('page') ? $request->input('page') : '1';

    $total = $fetch_data->total();
    $per_page = $fetch_data->perPage();
    $current_page = $fetch_data->currentPage();
    $last_page = $fetch_data->lastPage();
    $fisrt_page = $fetch_data->onFirstPage();
    $next_page_url = ($page == $last_page) ? null : $fetch_data->url($page+1);
    $prev_page_url = ($page == $fisrt_page) ? null : $fetch_data->url($page-1);
    $from = ($per_page * ($current_page-1)) + 1;

    $result = array(
    'total' => $total,
    'per_page' => $per_page,
    'current_page' => $current_page,
    'last_page' => $last_page,
    'next_page_url' => $next_page_url,
    'prev_page_url' => $prev_page_url,
    'from' => $from,
    'to' => $per_page,
    'data' => $in_data
    );

    return $result;
    }
}

No comments:

Powered by Blogger.