Home > OS >  Laravel pagination how to paginate database results
Laravel pagination how to paginate database results

Time:08-29

I want to paginate my array, here is one function in one of my controllers.

public function index()
{
    $data = DB::select("SELECT * FROM articles WHERE articleCategory = 'Economie' AND articleDate = CURDATE() ORDER BY articleDate DESC, articleHour DESC, articleMinute ASC, articleSeconde ASC");
    return view('business', compact('data'));
}

I tried to do this :

$data = DB::select("SELECT * FROM articles WHERE articleCategory = 'Economie' AND articleDate = CURDATE() ORDER BY articleDate DESC, articleHour DESC, articleMinute ASC, articleSeconde ASC")->paginate(10);

But Laravel didn't recognize paginate() --> Call to a member function paginate() on array

CodePudding user response:

$data = DB::table('articles')
    ->where('articleCategory', 'Economie')
    ->whereRaw('`articleDate` = CURDATE()')
    //alternatively ->where('articleDate', now()) 
    ->orderBy('articleDate', 'desc')
    ->orderBy('articleHour', 'desc')
    ->orderBy('articleMinute', 'asc')
    ->orderBy('articleSeconde', 'asc')
    ->paginate();

CodePudding user response:

I used this way.

 $data=Data::paginate(12);


$data=Data::orderBy('id', 'Asc')->paginate(12);

Data is Model name and 12 is number is number of paginate

  • Related