I’m creating project with Laravel. I want to pull out user->name
from subscription table which also has user_id
column in it. I want to show the current user’s subscribers’ name from subscription table. But it shows error:
Property [subs] does not exist on this collection instance
here is my code.
This is from IndexController
public function home() {
$data = User::all();
return view("amvs.index", [
'users' => $data,
]);
}
This is from index.blade.php
<a href="#" class="list-group-item list-group-item-action border rounded-0">Subscriptions</a>
@foreach ($users->subs as $sub)
<span class="ms-2">{{$subs->name}}</span>
@endforeach
This is from user model
public function sub(){
return $this->hasMany('AppModelsSubscription');
}
I didn’t write anything in Subscription model. My subscription table
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('user_id');
$table->timestamps();
});
My user table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
I’m a beginner so enlighten me.
2
Answers
First of all, In your controller you should load the subs. That will be better as it will not add extra query for loading the relations. So what you can do is following
you should also change the relationship as hasMany follows the following convention:
As you are pulling all the users you have to first loop through the users first, then you can iterate over subs for each users like below:
The issue is that you’re trying to access a method on an Eloquent Collection which does not have any properties or methods relatated to your
User
or itssub
relationship.Additionally, consider using Eager Loading when accessing relationships, it is more performant and solves the
N+1
query problem.Then to access the
User
s subs in your view:However, you will want to change your
subscriptions
migration as theuser_id
field needs to be the sametype
as theid
field on yourusers
table.Further questions
For relationships to work, the field
type
(string
,integer
etc.) of theprimary
andforeign
key fieds must be the same. The default primary key in Laravel is theid
field and it has astype
ofunsigned big integer
. If you define aforeign
key field in a related table and itstype
is anything other thanunsignedBigInteger
the relationship will fail.foreignId
is simply a helper method provided by Laravel that creates a field with the correcttype
and the requiredforeign
key reference. Previously you had to do that all yourself, for example:The reason for using
first()
is that it returns aModel
rather than an Eloquent Collection. You’re only interested in oneUser
(the authenticatedUser
) so it is more efficient to usefirst()
rather thanget()
as you don’t then need to loop or extract the first item in the collection.You might want to spend some free time watching the Laravel from Scratch series. It is really detailed and covers lots of common scenarios.