#Why I face error while using pagination 'Call to undefined method App\Models\Product::links()"

34 messages · Page 1 of 1 (latest)

fading yew
#

anyone please?

south drift
granite gull
#

are you changing something about that $products in your view?

bronze sedge
fading yew
south drift
fading yew
#
    public function index()
    {
        $products = Product::paginate(5);
        return view('products.index',['products'=>$products]); 
       
    }

south drift
fading yew
#

how i can fix it , i am doing step by sep from video tutorial but end up on this error ,

south drift
fading yew
#

Sir I am beginner not professional but thanks anyway will find out

south drift
#

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.

fading yew
#
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>
south drift
#

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.

fading yew
south drift
#

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.

fading yew
#

thanks you sir

fading yew
#

why this function isn't working ```js
@section('script')
<script>
$(document).ready(function(){
alert(1);
})
</script>
@endsection

#

I want pop up window shwoing ip address and product_id and ok button

fading yew
#

@granite gull hello bro can you help me with this javascript issue

south drift
fading yew
#

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