Laravel分页视图

时间:2019-11-20 03:57:57

标签: php laravel eloquent laravel-6

大家好,我已经完成了分页编码,但是视图无法显示出来,有人可以引导我吗? 我的(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() !!}

MY error message

2 个答案:

答案 0 :(得分:1)

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

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


如果您使用parentparent_id的类别上建立了关系,则可以急于加载父项的描述:

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

答案 1 :(得分:0)

这是答案

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