【问题标题】:Laravel Pagination ViewLaravel 分页视图
【发布时间】:2019-11-20 03:57:57
【问题描述】:

大家好,我已经做了分页编码,但是视图调不出来,谁能指导我? 我的(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() !!}

【问题讨论】:

  • $category-&gt;render() 应该怎么做?渲染分页器? (这可能无论如何都行不通,因为您使用的是array,它没有分页)
  • 渲染函数在数组上不可用。它仅在集合中可用。对结果进行分页后,您将它们添加到数组中。这就是您收到该错误的原因。
  • 但即使我使用(
    )这仍然无法为我工作
    跨度>

标签: php laravel eloquent laravel-6


【解决方案1】:

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

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


如果您使用parent_id 在类别上为parent 设置了关系,您可以急切地加载父级的描述:

$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]);

【讨论】:

  • 感谢您回复我。我是 laravel 和 php 的新手,你能不能给我答案,因为我尝试了很多次,但没有找到解决方案
  • 我遵循了你的指导,我认为它正在工作,只是这个错误的一个小错误(尝试获取非对象的属性'描述'),如何解决这个问题
  • 表示某个类别的父级不存在
  • 如果一个类别没有parent_id,那么您必须在尝试使用父级之前检查...我更新了代码示例
  • 天哪,谢谢,我做到了……天哪,天哪,我花了 3 天时间,终于在你的帮助下我做到了……感谢上帝,耶稣爱你
【解决方案2】:

这就是答案

 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]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2021-08-02
    • 2020-03-14
    • 1970-01-01
    • 2018-04-10
    • 2017-12-23
    相关资源
    最近更新 更多