问题
Hi everyone, i have done the pagination coding, but the view couldn't call out, can someone guide me ? My (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'));
}
My 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() !!}
回答1:
You don't have a Paginator in your view. You are iterating the paginator in the Controller and building a new array of new Category instances. You are then passing that array to your view, not the paginated results.
Pass the paginator object to your view. An array doesn't have methods.
If you had a relationship setup on Category for parent
using the parent_id
you could eager load the description for the 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]);
You have adjusted the collection of items in the paginator and are returning the paginator.
If you only want to fix the paginator issue with what you have currently:
$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]);
回答2:
Here is the answer
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]);
}
来源:https://stackoverflow.com/questions/58946445/laravel-pagination-view