Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [inny]Laravel eloquent Relations with()
goartur
post
Post #1





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

Ostrzeżenie: (0%)
-----


Czesc, wpadlem w pewien maly klopot mam oto takie query przy pomocy eloquent:

  1. public function getWithCategories()
  2. {
  3. return Forum::with('categories')->whereHas('categories',function ($q){
  4. $q->where('is_visible', '=', 1);
  5. })->get();
  6. }



Niby wszystko jest ok, spowrotem otrzymuje json z Forumami i kategoriami przypisanymi do kazdego z for.
Problem polega na tym ze chce pokazac tez fora gdzie w tym momecie nie ma zadnej kategorii niestety przy pomocy powyzej podanego kodu laravel
zwraca jedynie rekordy ktore posiadaja kategorie.
Jest jakas funkcja ktora pozwoli mi zwrocic fora z kategoriami lub puste fora?

W dokumentacji nic nie widze:
https://laravel.com/docs/5.4/eloquent-relationships
Go to the top of the page
+Quote Post
IProSoft
post
Post #2





Grupa: Zarejestrowani
Postów: 479
Pomógł: 97
Dołączył: 6.09.2011
Skąd: php.net :)

Ostrzeżenie: (0%)
-----


Spróbuj:
  1. Forum::has('categories', '<', 1)->orWhereHas('categories', function ($q){
  2. $q->where('is_visible', '=', 1);
  3. })->get();

Go to the top of the page
+Quote Post
goartur
post
Post #3





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

Ostrzeżenie: (0%)
-----


Niestety nic nie pomoglo, jedyne co sie zmienilo to to ze w ogole nie pokazuje kategorii, tylko same fora.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


  1. Forum::with('categories')->where('categories.is_visible', 1)->get();

Coś takiego?
Go to the top of the page
+Quote Post
r4xz
post
Post #5





Grupa: Zarejestrowani
Postów: 673
Pomógł: 106
Dołączył: 31.12.2008

Ostrzeżenie: (0%)
-----


Trochę późno, ale dla potomnych jest o tym poświęcony mały podrozdział w dokumentacji "Constraining Eager Loads"

  1. return Forum::with(['categories' => function ($q){
  2. $q->where('is_visible', 1);
  3. }])->get();
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 13:08