how to access the defined relation in blade view laravel 5.6
Solution 1
get()
always returns a Collection
, with 0...N results. With this line:
$clients = Invoice::with('users')->get();
$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
Solution 2
Remove first()
method call, you don’t need this because you have retrieved the users
by doing:
{{ $clients->users->title }}
Note that if you want to use first()
call, you should change the query like:
{{ $client->users()->first()->title }}
Notice the difference between users
and users()
in fetching eloquent relationship.
Also since your relationship is hasOne, you should change users
to user
to avoid confusion.
Solution 3
You are returning a collection of clients so you need to iterate through the collection.
You are defining a hasOne relationship on your client model called users. First of all as its a hasOne relationship you should name it appropriately, in this case just user.
You can also define it much cleaner:
public function user()
{
return $this->hasOne(User::class);
}
Now your second issue is how you are trying to retrieve this relationship.
Lets say you are passing a collection of clients to your view, something like:
public function clients()
{
$clients = Client::with('user')->get();
return view('view.view.view', compact('clients');
}
In your blade you need to now iterate through your clients to get their user relationship:
@foreach($users as $user)
{{ $client->user->user_column_name }}
@endforeach
Hope that helps.
Just remember try and make your naming as easy to understand as possible, yeah this project might be for your eyes only but just this simple snippet of code becomes obscure for someone who's never seen it to get what's going on.
Related videos on Youtube
Farshad
Updated on June 04, 2022Comments
-
Farshad almost 2 years
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'); }
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)); }
and finally in my view i did this
<td>{{ $clients->users->first()->title }}</td>
but when i try to view i get this error
Property [users] does not exist on this collection instance
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" => "شسی"
-
Metalik almost 6 years
hasone
has typo, correct ishasOne
-
u_mulder almost 6 yearsFunction names are caseinsensitive
-
Farshad almost 6 yearsthat was the Auto complitation i corrected that but no that was not the problem still get the same error
-
-
Farshad almost 6 yearscomplete and clear :) thanks you saved me a lot of time and i learned from that , that was the part i was trying to understand between the collection of models and a signle model thanks thanks :)
-
Farshad almost 6 yearsjust 1 more question is that can i get the relation without the get() method ???$clients = Invoice::with('users'); like this is it correct to do ?
-
patricus almost 6 years@Farshad I'm not sure what you're trying to do (the syntax you've shown will not actually run a query). You may want to post another question with details about what you want. Post the link here and I'll take a look after I wake up, but someone else may answer before then.
-
revo almost 6 years@Farshad And more probably, for the sake of clearness, you need a
belongsTo
nothasOne
. -
Farshad almost 6 yearsthanks man that helped and one more question is it the same for hasmany relations ?
-
Farshad almost 6 yearspublic function products(){ return $this->hasMany('App\Product','id','product_id'); }
-
Farshad almost 6 years$products = Invoice::with('products')->get(); return view('admin.invoices.show', compact('invoice', $invoice),compact('clients',$clients))->with(compact('products',$products)); }
-
Farshad almost 6 years@foreach($products as $product) <td>{{ $product->product->name ?? 'no products' }}</td> @endforeach
-
Farshad almost 6 yearsso is it possible to do it like this and then send 2 variables that contain relations to view ?
-
Colin Barstow almost 6 yearsWhen you define your relationship my friend, simply do: return $this->relationshipType(ModelName::class);
-
Colin Barstow almost 6 yearsWhen you send data to the view all you need to do is one compact and the variable name e.g. return view('view', compact('invoices', 'clients', 'products');
-
Farshad almost 6 yearsoh thanks i really had problem with that :) thanks really :)
-
Farshad almost 6 yearscan u help me out with this question too ? stackoverflow.com/questions/50895038/…
-
Farshad almost 6 years@patricus can u help me out with this one too ? stackoverflow.com/questions/50895038/…
-
Michael Carolan almost 5 years@partricus - EXCELLENT answer... I've been a "dry and dirty" Laravel guy for about a year and expanding my horizons. This helped me with getting the relationship info passed into the view. Thanks!
-
user2682025 over 4 yearsIs there anyway to use latest() or anything else instead of first()? just for changing the first one