phplaravelpagination

Laravel pagination with problems


i have a simple question as i do not know which line i should add into to get laravel pagination. (->paginate(5).

public function index(Request $request)
{

    $codeSearch = $request->get('code');
    $descriptionSearch = $request->get('description');

    //$tmp = Category::all()->toArray();
    $tmp = Category::where('code','like','%' .$codeSearch. '%')->where('description','like','%' .$codeSearch. '%')->get()->toArray();

    $category = array();
    foreach ($tmp as $key => $row) {
        $policy = Category::find($row['parent_id']);

        $tmpResult = new Category();
        $tmpResult->id = $row['id'];
        $tmpResult->code = $row['code'];
        $tmpResult->description = $row['description'];
        $tmpResult->parent_id = $policy['description'];
        $tmpResult->status = $row['status'];

        array_push($category, $tmpResult);
    }

    return view('category.index', compact('category'));
}

Solution

  • Try

     $tmp = Category::where('code','like','%' .$codeSearch. '%')->where('description','like','%' .$codeSearch. '%')->paginate(5);
    

    In view

     @foreach($tmp as $tm)
        //whatever operation you like to do
     @endforeach
     {{$tmp->links()}}