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 -->

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

Eloquent: Relationships - Laravel, Master, 7.x, 6.x · 5.8 · 5.7 · 5.6 · 5.5 · 5.4 · 5.3 · 5.2 · 5.1 · 5.0 · 4.2 Eloquent relationships are defined as methods on your Eloquent model classes. Since Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Let's look at the tables necessary to define this relationship: A one-to-one polymorphic relation is similar to a simple one-to-one relation; however, the target model can belong to more than one type of model on a single association. For example, a blog Post and a User may share a polymorphic relation to an Image model.


You should try this

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

Views - Laravel, Views. Creating Views; Passing Data To Views. Sharing Data With All Views. View Composers Check out the full Blade documentation to get started. Views​  Has Many Through relationship is a bit complicated to understand a provide shortcut way to access data of another mode relation. For example, a country is connected with users and users with posts, then we can access all posts connected with a specific country.


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

Eloquent ORM - Laravel, Doing this will let you catch the exception so you can log and display an error Like with withTrashed , the restore method may also be used on relationships: What i want to do is that i want to print out all the tours on the index page. But i only want to take one TourImage file to display for each tour. I return only array of all tours from controller. Is there a way that i can retrieve image directly on the blade view without returning more variable from the controller?


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

Eloquent: Collections - Laravel, The fresh method retrieves a fresh instance of each model in the collection from the database. In addition, any specified relationships will be eager loaded: For retrieve data from MySQL database using laravel framework first we have to create a table in data base. After create a table in the MySQL database you need to insert record or data on it.If you want to know how to insert data in laravel framework please visit the link : Insert data in Laravel .


Eloquent: Relationships 5.6 Laravel, Eloquent relationships are defined as methods on your Eloquent model classes. Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic Let's look at the tables required to define this relationship: Eloquent provides one Relation type for far related tables – hasManyThrough. However it works only with with cascade of hasOne/hasMany relations and you can use it only for 2 levels of nesting, while sometimes you need more. Imagine, that you want to get the posts for logged-in user, having this setup:


Laravel One to One Eloquent Relationship Tutorial, we will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc. In this example, i will  Laravel 5 5 tutorial 24 how to retrieve one record from the database in laravel IMPORTANT LINKS: ASK any questions about this video and any other business and get support by posting your question


Laravel 5.7 tutorial, I want to get relationship data into json using Resource in laravel 5.6 When I query, Posted 1 year ago by gohilronak using API Resources) But with API Resources it is only showing id and not Take a look at this tutorial:  One to Many relationship will use when one table associated with multiple tables. For example, a post may have multiple comments. So in this tutorial, you can understand how to create migration with a foreign key schema for one to many relationships, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many relationships.