Laravel 分页视图

Laravel Pagination View

大家好,我做了分页编码,但是view调不出来,谁能指导一下? 我的 (categoryController.php) :

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

    $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'));
}

我的 index.blade.php :

@foreach($category as $row)

                <tr>
                    <td>{{$row['id']}}</td>
                    <td>{{$row['code']}}</td>
                    <td>{{$row['description']}}</td>
                    <td>{{$row['parent_id']}}</td>
                    <td>{{$row['status']}}</td>

                    <td><a href="{{action('categoryController@edit', $row['id'])}}" class="btn btn-warning">Edit</a></td>
                    <td>
                        <form method="post" class="delete_form" action="{{action('categoryController@destroy',$row['id'])}}">
                            {{  csrf_field()    }}
                            {{  method_field('DELETE')}}

                            <input type="hidden" name="_method" value="DELETE"  />
                            <button type="submit" class="btn btn-danger">Delete</button>
                        </form>
                    </td>
                </tr>
            @endforeach
        </table>
        <div class="container">
            @foreach ($category as $row)
                {{ $row->name }}
            @endforeach
        </div>
        {!! $category->render() !!}

您的视图中没有分页器。您正在 Controller 中迭代分页器并构建新的 Category 实例数组。然后您将该数组传递给您的视图,而不是分页结果。

将分页器对象传递给您的视图。数组没有方法。


如果您使用 parent_idparent 的类别上设置了关系,您可以预先加载父级的描述:

$categories = Category::with('parent:id,description')->....->paginate(5);

foreach ($categories as $category) {
    if ($category->parent_id) {
        $category->parent_id = $category->parent->description;
    }
}

return view(..., ['categories' => $categories]);

您已调整分页器中的项目集合并返回分页器。

如果您只想解决当前拥有的分页器问题:

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

foreach ($categories as $category) {
    if ($policy = Category::find($category->parent_id, 'description')) {
        $category->parent_id = $policy->description;
    } 
}

return view('category.index', ['category' => $categories]);

答案在这里

 public function index(Request $request)
{
    $codeSearch = $request->get('code');
    $descriptionSearch = $request->get('description');

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

    foreach ($categories as $category) {
        if ($policy = Category::find($category->parent_id, 'description')) {
            $category->parent_id = $policy->description;
        }
    }

    return view('category.index', ['category' => $categories]);
}