简体   繁体   English

Laravel 5.4关系beLongsToMany

[英]Laravel 5.4 relationship beLongsToMany

Hi i need fix returning view. 嗨,我需要修复返回视图。 I made relation and its returning array. 我建立了关系及其返回数组。 How i can change this and i am not sure i made good function to relationship. 我该如何改变这一点,我不确定我对关系的作用。 I just trying lerning it i saw many tutorials and i know in Laravel is so kind magic tips to returning. 我只是想知道它,所以我看到了许多教程,而且我知道在Laravel中,返回的技巧非常好。 My function must showing events what user was joining. 我的函数必须显示事件,说明用户正在加入。 I think i make it but its returning array and when i try do something like that 我认为我做到了,但是它返回了数组,当我尝试做类似的事情时

 @foreach($zapisevents as $zapisevent) <table class="table"> <th>{{$zapisevent->eventsave->name}}</th> </table> @endforeach 

i got error: Property [name] does not exist on this collection instance. 我得到了错误: 此集合实例上不存在属性[name]。 (View: /home/mariusz/Pulpit/www/szpital/resources/views/profil/profil.blade.php) (查看:/home/mariusz/Pulpit/www/szpital/resources/views/profil/profil.blade.php)

but when i use <th>{{$zapisevent->eventsave}}</th> its returning array. 但是当我使用<th>{{$zapisevent->eventsave}}</th>它会返回数组。

There is function for joining to event 有加入活动的功能

  public function index() { $userid = Auth::user(); $zapisevents = User::with('eventsave')->where('id',(Auth::user()->id))->get(); return view('profil.profil', ['userid' => $userid], ['zapisevents' => $zapisevents]); } 
Model User: 模型用户:

  public function eventsave() { return $this->belongsToMany(HomeModel::class,'save_events','users_id','events_id')->withTimestamps(); } 

Model HomeModel <<< 模特首页模特<<<

  public function usersave() { return $this->belongsToMany(User::class,'save_events','events_id','users_id'); } 
Its returning: 返回:

[{"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"}},{"id":7,"name":"kkkkkkkkkkkkkkkkkkkkkkkk","title":"Wydzial 4","start":"2017-04-01 00:00:00","end":"2017-04-01 23:59:59","created_at":"2017-04-01 19:54:24","updated_at":"2017-04-01 19:54:24","pivot":{"users_id":3,"events_id":7,"created_at":"2017-04-01 19:55:41","updated_at":"2017-04-01 19:55:41"}}] [{“ id”:5,“名称”:“ asdasdsa”,“标题”:“ Wydzial 1”,“开始”:“ 2017-04-04 03:00:00”,“结束”:“ 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“}},{” id“ :7,“名称”:“ kkkkkkkkkkkkkkkkkkkkkkkkkk”,“标题”:“ Wydzial 4”,“开始”:“ 2017-04-01 00:00:00”,“结束”:“ 2017-04-01 23:59 :59“,” created_at“:” 2017-04-01 19:54:24“,” updated_at“:” 2017-04-01 19:54:24“,”数据透视“:{” users_id“:3,” events_id“:7,” created_at“:” 2017-04-01 19:55:41“,” updated_at“:” 2017-04-01 19:55:41“}}]

the

@foreach($zapisevents as $zapisevent)
  <table class="table">
  <th>{{$zapisevent->eventsave->name}}</th>
  </table>
  @endforeach

Should ne 应否

@foreach($zapisevents as $zapisevent)
  <table class="table">
 @foreach($zapisevent->eventsave as $eventSave)
  <th>{{$eventsave->name}}</th>
@endForeach
  </table>
  @endforeach

in you code the name property is being called in a collection of HomeModel but it needs to be called in a model itself 在您的代码中,名称属性在HomeModel的集合中被调用,但是它需要在模型本身中被调用

When using arrays, you need to access their properties via their index like this: 使用数组时,您需要通过它们的索引访问它们的属性,如下所示:

$zapisevent->eventsave['name']

as opposed to: 相对于:

$zapisevent->eventsave->name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM