Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Laravel 5.4 relacje, Problem z prawidłowym wyświetlaniem relacji
K3n0
post
Post #1





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 3.05.2016

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


Witam mam do zrobienia zapisywanie się do eventu. Zrobilem to na relacji Many to Many i wszystko jest ok uzytkownik sie zapisuje i w zakladce profil wyswietla sie na jakie eventy się zapisał, ale niestety wyświetla mi się wszystko jako array i nie wiem jak to zrobic zeby pokazywalo normalnie jako string. Jak czytalem to w laravelu mozna wykorzystać:
  1. {{$zapisevent->eventsave->name}}

Ale jak korzystam z tego to mam error, że niby name nie istnieje:
Property [name] does not exist on this collection instance. (View: /home/mariusz/Pulpit/www/szpital/resources/views/profil/profil.blade.php)

Więc wyświetlam relacje w taki sposób:
  1. @foreach($zapisevents as $zapisevent)
  2. <table class="table">
  3.  
  4. <th>{{$zapisevent->eventsave->name}}</th>
  5. </table>
  6. @endforeach


Controller do wyświetlania relacji:
  1. public function index()
  2. {
  3. $userid = Auth::user();
  4.  
  5.  
  6. $zapisevents = User::with('eventsave')->where('id',(Auth::user()->id))->get();
  7. return view('profil.profil', ['userid' => $userid], ['zapisevents' => $zapisevents]);
  8.  
  9. }


Model User:
  1. public function eventsave()
  2. {
  3. return $this->belongsToMany(HomeModel::class,'save_events','users_id','events_id')->withTimestamps();
  4. }

Model HomeModel <- odpowiedzialny za eventy:
  1. public function usersave()
  2. {
  3. return $this->belongsToMany(User::class,'save_events','events_id','users_id');
  4. }
Go to the top of the page
+Quote Post
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
Ale jak korzystam z tego to mam error, że niby name nie istnieje:
To nie mozesz sobie wyswietlic tej tablicy i zobaczyc co tam faktycznie sie znajduje?


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

"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
K3n0
post
Post #3





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 3.05.2016

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


Wyświetlam i jest ok to co trzeba ale pokazuje wszystko jako tablice:

[{"id":5,"name":"asdasdsa","title":"Wydzial 1","start":"2017-04-04 03:00:00","end":"2017-04-04 07:59:00","created_at":"2017-04-01 18:50:40","updated_at":"2017-04-01 18:50:40","pivot":{"users_id":3,"events_id":5,"created_at":"2017-04-01 18:50:58","updated_at":"2017-04-01 18:50:58"}},

To pokazuje sie gdy nie korzystam z name tylko daje: $zapisevent->eventsave

Ten post edytował K3n0 4.04.2017, 16:31:06
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Tam masz tekst json a nie tablice


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

"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
K3n0
post
Post #5





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 3.05.2016

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


Już naprawiłem dzięki i tak za pomoc
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




No to pochwal sie rozwiazaniem. Moze ktos inny skorzysta


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

"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
K3n0
post
Post #7





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 3.05.2016

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


Tutaj rozwiązanie:
  1. <table class="table table-striped">
  2. <thead>
  3. <tr>
  4. <th>#</th>
  5. <th>Nazwa</th>
  6. <th>Wydział</th>
  7. <th>Rozpoczęcie</th>
  8. <th>Zakończenie</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <?php $i = 1;?>
  13. @foreach($zapisevents as $zapisevent)
  14. @foreach($zapisevent->eventsave as $eventsave)
  15. <tr>
  16. <th scope="row">{{ $i++ }}</th>
  17. <td>{{$eventsave->name}}</td>
  18. <td>{{$eventsave->title}}</td>
  19. <td>{{$eventsave->start}}</td>
  20. <td>{{$eventsave->end}}</td>
  21. </tr>
  22. @endforeach
  23. </table>
  24. @endforeach


Ten post edytował K3n0 4.04.2017, 16:54:55
Go to the top of the page
+Quote Post
nospor
post
Post #8





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




table zamykajace bez sensu lezy miedzy dwoma foreach i ucina tabele ni z gruszki ni z pietruszki.


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

"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 Aktualny czas: 22.08.2025 - 11:31