Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> 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
 
Start new topic
Odpowiedzi
K3n0
post
Post #2





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

Posty w temacie


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: 15.10.2025 - 03:43