how to access the defined relation in blade view laravel 5.6

Far*_*had 2 php controller view laravel laravel-blade

i want to access the defined relation in my blade view and show it i am doing like this in my invoice model

public function users() {
    return $this->hasone('App\Client','id','client_id');
}
Run Code Online (Sandbox Code Playgroud)

and here in invoice controller

 public function show(Invoice $invoice)
{
    $clients = Invoice::with('users')->get();
    return view('admin.invoices.show', compact('invoice', $invoice),compact('clients',$clients));
}
Run Code Online (Sandbox Code Playgroud)

and finally in my view i did this

<td>{{ $clients->users->first()->title }}</td>
Run Code Online (Sandbox Code Playgroud)

but when i try to view i get this error

Property [users] does not exist on this collection instance
Run Code Online (Sandbox Code Playgroud)

when i dd the $clients i get results in relation like below

 #relations: array:1 [?
    "users" => Client {#309 ?
      #fillable: array:14 [?]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:17 [?
        "id" => 1
        "title" => "???"
Run Code Online (Sandbox Code Playgroud)

pat*_*cus 5

get() always returns a Collection, with 0...N results. With this line:

$clients = Invoice::with('users')->get();
Run Code Online (Sandbox Code Playgroud)

$clients will be a Collection of Invoice objects. The users property doesn't exist on the Collection, it exists on each Invoice inside the Collection.

Make sure you're looping through your $clients collection and accessing users on the individual Invoice items.


NB: While not your primary issue here, @Joshua's note about the users is correct. The hasOne relationship will return either a Model instance if the relationship exists or null if the relationship doesn't exist. I would also suggest changing the name from users to user, and maybe adding some protection for when the relationship doesn't exist:

@foreach($clients as $client)
    ...
        <td>{{ $client->user->title ?? 'No Title' }}</td>
    ...
@endforeach
Run Code Online (Sandbox Code Playgroud)