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

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: 25.09.2025 - 18:54