Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]json w laravel
Puchatek320
post 18.05.2020, 11:27:57
Post #1





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


Cześć,
Mam głupi problem i nie mogę zrozumieć o co chodzi.

Do rzeczy

  1.  
  2. public function gettag(Request $request){
  3.  
  4. $searchTerm = $request -> get('term');
  5.  
  6. $tags = DB::table('tags')
  7. ->where('name', 'like', '%'.$searchTerm.'%')
  8. ->get();
  9.  
  10. $output = '<ul class="dropdown-menu"
  11. style="display:block;
  12. position:relative">';
  13. foreach ($tags as $data) {
  14.  
  15.  
  16. $output .='<li>'.$data->name.'<li>';
  17.  
  18. $data->name;
  19. $data->id;
  20.  
  21. }
  22.  
  23. $output .='<ul>';
  24.  
  25. echo $output;
  26. }
  27.  



zwraca 12 rekordów jeśli zrobię tak


  1.  
  2. public function gettag(Request $request){
  3.  
  4. $searchTerm = $request -> get('term');
  5.  
  6. $tags = DB::table('tags')
  7. ->where('name', 'like', '%'.$searchTerm.'%')
  8. ->get();
  9.  
  10.  
  11. foreach ($tags as $data) {
  12.  
  13. $data->name;
  14. $data->id;
  15.  
  16. }
  17.  
  18. return json_encode($data);
  19.  
  20.  
  21. }
  22. Nie rozumiem dlaczego zwraca ostatni rekord zamiast wszystkich
  23.  
Go to the top of the page
+Quote Post
viking
post 18.05.2020, 11:30:54
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


A co to niby jest zmienna $data?
https://laravel.com/docs/7.x/responses#json-responses


--------------------
Go to the top of the page
+Quote Post
Puchatek320
post 18.05.2020, 11:48:36
Post #3





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


zmienna $data jest tylko dla przykładu, Sprawdzałem już wcześniej dokumentację ale nie rozumiem jak ten przykład ma się do mojego ja wybieram dane z formularza a w przykładznie są statyczne


  1. return response()->json([
  2. 'name' => 'Abigail',
  3. 'state' => 'CA'
  4. ]);


normalnie w php robiłem tak,

  1. $dat = $data->name;
  2. json_encode($dat)


i było okey a tutaj nie rozumiem o co chodzi.
Go to the top of the page
+Quote Post
viking
post 18.05.2020, 12:06:12
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Przede wszystkim nie ma potrzeby jeździć foreachem tylko od razu zwróć dane z modelu. Po drugie https://laravel.com/docs/7.x/eloquent-resources
A po trzeba jeśli jednak miałby działać twój przykład to przed foreach zdefiniuj np $response = []; dodaj do niej dane i zwróć w response.
I jeszcze zapytanie jest podatne na podstawianie znaków specjalnych like.

Ten post edytował viking 18.05.2020, 12:06:38


--------------------
Go to the top of the page
+Quote Post
Puchatek320
post 18.05.2020, 12:55:06
Post #5





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


Coś takiego?


  1. $response = "";
  2. foreach ($tags as $data) {
  3.  
  4. $response = $data->name;
  5.  
  6.  
  7. }
  8.  
  9.  
  10. return json_encode($response);
  11.  
  12.  
  13. }
  14.  


co do pierwszej podpowiedzi według https://laravel.com/docs/7.x/queries

zwracają w foreach

  1. $titles = DB::table('roles')->pluck('title');
  2.  
  3. foreach ($titles as $title) {
  4. echo $title;
  5. }


ewentualnie czy mogę prosić jakiś przykład jak ma to wyglądać?
Go to the top of the page
+Quote Post
viking
post 18.05.2020, 12:56:58
Post #6





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


  1. $response = [];
  2. foreach ($tags as $data) {
  3. $response[] = $data->name;
  4. }
  5.  
  6. return json_encode($response);


  1. return response()
  2. ->json(['data' => $tags]);


--------------------
Go to the top of the page
+Quote Post
Puchatek320
post 18.05.2020, 13:11:04
Post #7





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


laravel zwracaj w json


  1. data [ {&#8230;}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
  2. 0 Object { name: "test" }
  3. name "test"
  4. 1 Object { name: "test1" }
  5. name "test1"
  6. 2 Object { name: "test2" }
  7. name "test2"


Jak osiągnąć


  1. 0 "test"
  2. 1 "test1"
  3. 2 "test2"


ponieważ w pierwszej opcji nie wyświetla się lista a autocomplete

Go to the top of the page
+Quote Post
viking
post 18.05.2020, 13:23:47
Post #8





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Poczytaj trochę o kolekcjach https://laravel.com/docs/7.x/collections#method-pluck


--------------------
Go to the top of the page
+Quote Post
Puchatek320
post 18.05.2020, 13:46:26
Post #9





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


Próbuje

use Illuminate\Http\Response;
  1. $searchTerm = $request -> get('term');
  2.  
  3. $tags = DB::table('tags')
  4. ->where('name', 'like', '%'.$searchTerm.'%')
  5. ->select('name')
  6. ->get();
  7.  
  8. $test = response()
  9. ->json(['data' => $tags]);
  10.  
  11.  
  12. $reversed = $test->reverse();
  13. $reversed->all();
  14.  


I wyrzuca Method Illuminate\Http\JsonResponse::reverse does not exist.
Go to the top of the page
+Quote Post
nospor
post 18.05.2020, 13:48:24
Post #10





Grupa: Moderatorzy
Postów: 36 441
Pomógł: 6290
Dołączył: 27.12.2004




A czemu wolasz reverse() na obiekcie RESPONSE? Patrz co piszesz troszke a nie strzelasz losowo kodami to kawalek stad, kawalek stamtad..


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Puchatek320
post 18.05.2020, 13:58:28
Post #11





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


jestem całkiem zielony jeśli chodzi o programowanie i czasami mam jakieś dziwne zboczenie że jeśli używałem $test później robię coś z kodem to wydaje mi się że wybiorę dane z samego początku a nie po obróbce...

  1. $searchTerm = $request -> get('term');
  2.  
  3. $tags = DB::table('tags')
  4. ->where('name', 'like', '%'.$searchTerm.'%')
  5. ->select('name')
  6. ->get();
  7.  
  8. response()
  9. ->json(['data' => $tags]);
  10.  
  11. $plucked = $tags->pluck('name');
  12. $plucked->all();
  13. echo $plucked;

Dzięki wielkie za pomoc.
Go to the top of the page
+Quote Post
viking
post 18.05.2020, 14:05:01
Post #12





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Ten kod wyżej i tak nie ma sensu.


--------------------
Go to the top of the page
+Quote Post
Puchatek320
post 18.05.2020, 14:10:33
Post #13





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 4.11.2019

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


dlaczego?
Go to the top of the page
+Quote Post
nospor
post 18.05.2020, 14:45:54
Post #14





Grupa: Moderatorzy
Postów: 36 441
Pomógł: 6290
Dołączył: 27.12.2004




Bo robisz
response()
->json(['data' => $tags]);
na danych z oryginalnej tablicy a dopiero potem na tej kolekcji robisz pluck.
json reponse masz robic na danych po PLUCK a nie przed.

+ nic z tym reponse nie robisz wiec on raczej leci w kosmos


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 12:53