Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Laravel 5.4 relacje
Forum PHP.pl > Forum > PHP
K3n0
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. }
nospor
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?
K3n0
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
nospor
Tam masz tekst json a nie tablice
K3n0
Już naprawiłem dzięki i tak za pomoc
nospor
No to pochwal sie rozwiazaniem. Moze ktos inny skorzysta
K3n0
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
nospor
table zamykajace bez sensu lezy miedzy dwoma foreach i ucina tabele ni z gruszki ni z pietruszki.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.