with this?
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria)->orderBy('id', 'desc')->firstOrFail();
$produkty_po_subkategorii->paginate(8);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello! I have:
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria)->firstOrFail()->products;
$produkty_po_subkategorii->orderBy('id', 'desc')->paginate(8);
Return: Method orderBy does not exist.
When I use without orderBy and paginate this is works.
with this?
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria)->orderBy('id', 'desc')->firstOrFail();
$produkty_po_subkategorii->paginate(8);
Undefined property: Illuminate\Database\Eloquent\Builder::$products
you add that . I forgot to remove
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria)->orderBy('id', 'desc')->paginate(8);
I don't think you can paginate() after already retrieving the data with get() (or findOrFail, etc). Just use paginate() instead of get(), etc.
@Cronix you are right
It works!
{"current_page":1,"data":[{"id":2,"name":"\u015arubokr\u0119ty","created_at":"2018-04-09 11:03:36","updated_at":"2018-04-09 11:03:36","products":[]}],"first_page_url":"http:\/\/mesiek.pl\/magazyn\/narz%C4%99dzia\/%C5%9Brubokr%C4%99ty?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/mesiek.pl\/magazyn\/narz%C4%99dzia\/%C5%9Brubokr%C4%99ty?page=1","next_page_url":null,"path":"http:\/\/mesiek.pl\/magazyn\/narz%C4%99dzia\/%C5%9Brubokr%C4%99ty","per_page":8,"prev_page_url":null,"to":1,"total":1}
I want to get products without "id":2,"name":"\u015arubokr\u0119ty" (subcategory)
Then add a select to the query and grab only the fields you want. You are retrieving all fields because you aren't specifying which ones to get.
But I think you need to get the id field. Doesn't your products relationship need it to get the products for each subcategory?
return $produkty_po_subkategorii->current_page->products;
or
return $produkty_po_subkategorii->products;
or
return $produkty_po_subkategorii->current_page->data->products;
Doesn't works.
currentPage()
What exactly are you trying to do? Why are you using pagination at all if you just want to return some specific data? It's not looking like you actually want to use pagination.
You must have misunderstood me.
When I use:
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria)->orderBy('id', 'desc')->paginate(8);
Return: {"current_page":1,"data":[{"id":3,"name":"Klucze","created_at":"2018-04-09 11:03:40","updated_at":"2018-04-09 11:03:40","products":[]}],"first_page_url":"http://mesiek.pl/magazyn/narz%C4%99dzia/klucze?page=1","from":1,"last_page":1,"last_page_url":"http://mesiek.pl/magazyn/narz%C4%99dzia/klucze?page=1","next_page_url":null,"path":"http://mesiek.pl/magazyn/narz%C4%99dzia/klucze","per_page":8,"prev_page_url":null,"to":1,"total":1}
I want to get only an array of products.
Cronix I have a sub-category and a product in them. I do not want all products to be displayed.
I'm only going by what you're posting, and
return $produkty_po_subkategorii->current_page->products;
or
return $produkty_po_subkategorii->products;
or
return $produkty_po_subkategorii->current_page->data->products;
makes absolutely no sense if you're using pagination. So I was asking for clarity on what you are actually trying to do. The only thing you should be returning is return view('your_view_name', compact('produkty_po_subkategorii'); and then looping through your data, like you normally would to display it, and add the pagination links in the view like is shown in the manual.
@foreach ($produkty_po_subkategorii->products as $product)
// show product details
@endforeach
// display pagination links
{{ $produkty_po_subkategorii->links() }}
Initially, I had:
public function sklep()
{
$s_counts = Product::latest()->count();
$query = Input::get('s');
$liczba_paginacji = 8;
$zapytanie = Product::where('nazwa', 'like', '%' . $query . '%');
$search_results = $zapytanie->orderBy('id', 'desc')->paginate($liczba_paginacji);
// amount paginate
$ile_stron = $s_counts / $liczba_paginacji;
// Category
$categories = MagazineCategory::all('name', 'id');
$subcategories = MagazineSubcategory::all('name', 'id');
return view('strona_glowna.sklep', compact('search_results', 's_counts' ,'query', 'liczba_paginacji', 'ile_stron', 'categories', 'subcategories'));
}
View:
@foreach($search_results as $result)
<div class="col-lg-3 col-md-3">
<div class="mm-produkty-jeden">
<a href="{{ url('magazyn/produkt', $result->id) }}" class="d-flex justify-content-center mm-produkty-link-grafika">
<div class="row">
<div class="col">
<img src="{{ url('/obrazy/produkty/' . $result->id . '/300x300/90') }}" alt="" class="mm-produkty-grafika img-fluid">
</div>
</div>
</a>
<a href="{{ url('magazyn/produkt', $result->id) }}" class="mm-produkty-link">
<h5 class="text-center">{{ $result->nazwa }}</h5>
</a>
<p class="text-center mm-produkty-opis">{!! str_limit(strip_tags($result->kopis), 80) !!}</p>
<h5 class="text-warning text-uppercase text-center mm-produkty-cena">{{ $result->cena }} PLN</h5>
</div>
</div>
@endforeach
It only downloaded products from the database. Now I want to do the same with grouping (subcategories).
I have: Controller:
$liczba_paginacji = 8;
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria)->orderBy('id', 'desc')->paginate($liczba_paginacji);
return view('strona_glowna.sklep_sub', compact('search_results');
View:
@foreach($search_results as $result)
{{ $result }}
@endforeach
Return:
{"id":1,"name":"Wiertarki","created_at":"2018-04-09 11:03:31","updated_at":"2018-04-09 11:03:31","products":[{"id":1,"nazwa":"Wiertarka #1","cena":543,"kopis":"<p>Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Nulla porttitor accumsan tincidunt.<\/p>","dopis":"<h3>Nulla porttitor accumsan tincidunt. Proin eget tortor risus. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.<\/h3>\r\n<p> <\/p>\r\n<p>Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla quis lorem ut libero malesuada feugiat. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus.<\/p>","n_img":"kTQSPLS6SlOASD2OtvwFjuvIbXplSibZCcjmKLvl.png","created_at":"2018-04-09 11:04:32","updated_at":"2018-04-09 11:04:32","pivot":{"category_id":1,"subcategory_id":1}}]}
Continue inside... Controller unchanged. View:
@foreach($search_results as $result)
{{ $result->products }}
@endforeach
Return:
[{"id":1,"nazwa":"Wiertarka #1","cena":543,"kopis":"<p>Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Nulla porttitor accumsan tincidunt.<\/p>","dopis":"<h3>Nulla porttitor accumsan tincidunt. Proin eget tortor risus. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.<\/h3>\r\n<p> <\/p>\r\n<p>Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla quis lorem ut libero malesuada feugiat. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus.<\/p>","n_img":"kTQSPLS6SlOASD2OtvwFjuvIbXplSibZCcjmKLvl.png","created_at":"2018-04-09 11:04:32","updated_at":"2018-04-09 11:04:32","pivot":{"category_id":1,"subcategory_id":1}}]
Continue inside... Controller unchanged. View:
@foreach($search_results as $result)
{{ $result->products->nazwa }}
@endforeach
Return:
Error: Property [nazwa] does not exist on this collection instance. (View: C:\xampp\htdocs\projekt_pelka\resources\views\strona_glowna\sklep_sub.blade.php)
I can not get product information.
Products is an array of many products. So you have to iterate over them.
@foreach($search_results as $result)
@foreach ($result->products as $product)
{{ $product->nazwa }}
@endforeach
@endforeach
A moment ago I found a thread about the collection :) And your solution is also correct.
Thank you for your time for me!
You're welcome!
And if I want to get to the collection but on the controller side?
I don't know what you mean. Whatever collection variable you are sending to the view is the same collection in your controller.
I have:
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria_z_malej_pl)->orderBy('id', 'desc')->paginate($liczba_paginacji);
return $produkty_po_subkategorii;
Return:
{"current_page":1,"data":[{"id":1,"name":"Wiertarki","created_at":"2018-04-09 11:03:31","updated_at":"2018-04-09 11:03:31","products":[{"id":1,"nazwa":"Wiertarka #1","cena":543,"kopis":"qwe","dopis":"qwe","n_img":"kTQSPLS6SlOASD2OtvwFjuvIbXplSibZCcjmKLvl.png","created_at":"2018-04-09 11:04:32","updated_at":"2018-04-09 11:04:32","pivot":{"category_id":1,"subcategory_id":1}},{"id":2,"nazwa":"\u015arubokr\u0119t #1","cena":123,"kopis":"qwe","dopis":"qwe","n_img":"xRNCCQnsDoyAD27HKnhuN3d80Gq8johi9pPiTFbY.png","created_at":"2018-04-09 22:03:48","updated_at":"2018-04-09 22:05:09","pivot":{"category_id":1,"subcategory_id":2}},{"id":3,"nazwa":"Wiertarka #2","cena":324,"kopis":"qwe","dopis":"qwe","n_img":"306EfUCk8JyhbwCV5nKPVGE0JJG4RJOD0leqgLBn.png","created_at":"2018-04-09 22:04:02","updated_at":"2018-04-09 22:05:44","pivot":{"category_id":1,"subcategory_id":3}}]}],"first_page_url":"http:\/\/mesiek.pl\/magazyn\/narz%C4%99dzia\/wiertarki?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/mesiek.pl\/magazyn\/narz%C4%99dzia\/wiertarki?page=1","next_page_url":null,"path":"http:\/\/mesiek.pl\/magazyn\/narz%C4%99dzia\/wiertarki","per_page":8,"prev_page_url":null,"to":1,"total":1}
I would like to count the number of products.
$produkty_po_subkategorii = MagazineSubcategory::with('products')->where('name', $subkategoria_z_malej_pl)->orderBy('id', 'desc')->paginate($liczba_paginacji);
return $produkty_po_subkategorii->products->count();
Return:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$products
Why do you need to return the count to the view, when you can just get the count in the view?
{{ $produkty_po_subkategorii->products->count() }}
Suggestion: $produkty_po_subkategorii->getCount('products') or $produkty_po_subkategorii->count('products')
One.
return $produkty_po_subkategorii->getCount('products');
Result: Method getCount does not exist.
Two.
return $produkty_po_subkategorii->count('products');
Return: 1
I have more products. Certainly not one.
You didn't say you wanted a count of all products, and that's not what your code was showing you trying to do. It really helps to explain yourself.
This is in the user guide: https://laravel.com/docs/5.6/eloquent-relationships#counting-related-models
I urge you to read all of the manual. It makes things easier.
Please or to participate in this conversation.