#Why I face error while using pagination 'Call to undefined method App\Models\Product::links()"
34 messages · Page 1 of 1 (latest)
You posted in what is the early hours in Europe on a Sunday. Be patient, man.
are you changing something about that $products in your view?
https://laravel.com/docs/10.x/pagination#displaying-pagination-results
I have a hunch:
{{ $product->links() }}
You might have that in your code to the pagination. Calling it with the singular variable (Eloquent Model) instead of $products, the LengthAwarePaginator.
no just showing
I am following same steps , but still it give Call to undefined method App\Models\Product::onEachSide()
You’re clearly calling those methods on a model, and not on a paginator.
what do you mean by that i had $products on ProductController
public function index()
{
$products = Product::paginate(5);
return view('products.index',['products'=>$products]);
}
I mean, look at your error. It’s clearly telling you you’re trying to call paginator methods on a model.
how i can fix it , i am doing step by sep from video tutorial but end up on this error ,
By calling methods on a paginator and not a single model.
Sir I am beginner not professional but thanks anyway will find out
Well show your controller and view as it is now?
We can’t do much when all you present is an error telling us (and you) that you’re calling methods on a model instance and not a paginator.
The solution is to not do that.
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$products = Product::paginate(5);
return view('products.index',['products'=>$products]);
}
<form action="{{ route('products.destroy', $products->id) }}" method="POST">
@csrf
@method('delete')
<p class="form-group">Are you sure to delete {{ $products->Title }} ? </p>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-block " data-bs-dismiss="modal" style="float: left">Cancel</button>
<button type="submit" class="btn btn-secondary btn-danger">Delete Products</button>
</div>
</form>
</div>
</div>
</div>
</div> <!---end delete modal---->
@endforeach
</tbody>
</table>
<div class="row">
{{-- $products::links('pagination::bootstrap-4') --}}
{{ $products->onEachSide(5)->links() }}
</div>{{-- Pagiantion end --}}
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card">
<div class="card-header"><h4>Search Products</h4>
<div class="card-body">
...........
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You’re clearly assigning a single product to $products somewhere if you’re also printing $products->Title
So again, show the full view, and not just a snippet that you think is relevant.
So yes, the problem is your foreach.
You’re just overwriting the $products variable with a single instance of every iteration.
You usually do something like @foreach($products as $product)
Instead of using the exact same variable name for the iteration, otherwise you end up with errors like you’re getting.
thanks you sir
can i ask you one more question sir?
why this function isn't working ```js
@section('script')
<script>
$(document).ready(function(){
alert(1);
})
</script>
@endsection
complete code is of receipt page"
app.blade.php is
I want pop up window shwoing ip address and product_id and ok button
@granite gull hello bro can you help me with this javascript issue
You need to learn some patience. Stop bumping posts and stop randomly tagging people.
from where ?, I am learning from many YouTube channels, following same step carefully end up with some error ? and English is not my first languageso Documents are not helpful for me