简体   繁体   中英

How to retrieve a Has One relation in view in Laravel 5.6

I have a relationship where a user has many emails, and an email belongs to one user. I define it like this in my message model :

public function users() {
    return $this->belongsTo('App\User','sender_id','id');
}

Here is my controller :

 $sender_name = Message::with('users')->get();
 return view('admin.message.read',compact('message','sender_name'));

In my view, I do something like this :

 @foreach($sender_name as $sender_names)
  @php(
  dd($sender_name)
  )
    {{$sender_name->name}}
 @endforeach

So, here I have 2 questions:

  1. How do I retrieve the relation I defined in my view? Currently, I get an error that this property does not exist on collection !!!
  2. Do I need to define the opposite relation in the user's model like this? :
    public function messages() {
        $this->hasOne('App\Message','id','sender_id');
    }

Here is the code around the view :

<div class="media-body">
    <h6 class="media-heading">{{$message->title}}</h6>
    <!-- sender name of Email-->
    <div class="letter-icon-title text-semibold">
        @foreach($sender_name as $sender_names)
            {{$sender_names->users->name}}
        @endforeach
    <!-- sender name of Email-->

    <!-- sender Email-->
        <a href="#">jira@diakultd.atlassian.net</a>
    </div>
    <!-- sender name of Email-->
</div>
<!-- body of Email-->
<table width="600" border="0" cellpadding="0" cellspacing="0" align="center">
    <tr>
        <td valign="middle" align="center" style="font-size: 40px; color: #ffffff; line-height: 50px; font-weight: 300;">   
        </td>
    </tr>
</table>
<!-- /title -->

You should try this

@foreach($sender_name as $sender_names)  
    {{$sender_names->users->name}}
@endforeach

As you want to get the user who have added this message. So for that you are fetching messages through its model and adding up users in it through relation, that's why you need to define it in messages model.

To use it in views you can use

@foreach($sender_name as $sender_names)
    {{$sender_names->users->name}}
@endforeach

To study about relations Laravel has a good documentation https://laravel.com/docs/5.7/eloquent-relationships

you can try like that

@foreach($sender_name as $sender_names)  
    {{$sender_names->users->name}}
@endforeach

see this is good example of one to one relationship

so here is what happened to me in view : the foreach loops that people said was true but i had some other problem that i could do without foreach to show a sender id

public function show(Message $message)                                                                       
 {                                                                                                                                                                                                                       
     $sender_name = Message::with('users')->where('sender_id', $message->sender_id)->first();                 
        Message::with('users')->get();                                                                        
     return view('admin.message.read',compact('message','sender_name'));                                      
 } 

and simple as that in view you print it

{{$sender_name->users->name}}

and like this you can print the name of the sender of the email

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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