Anyone now to fix ths Method Illuminate\Auth\SessionGuard::sondazhe does not exist. I`m trying to show users session on my view but what i get is this. Anyone now how to fix this
view
<div>
<h3>
:: {{ auth()->sondazhe()->user_id }}
</h3>
</div>
users model
public function sondazhes() {
return $this->hasMany(Role::class);
}
sondazhe model
public function roles()
{
return $this->belongsToMany(Role::class);
}
role model
public function sondazhes()
{
return $this->belongsToMany(Sondazhe::class);
}
public function users()
{
return $this->belongsToMany(User::class);
}
Did you mean to do
auth()->user()->sondazhe->first()->user_id
Is not working show an error of onon-object
Sondazhe is my table where i have stored some data and now i want to get this data for specific user based on user_id i think
Updated.. And remember to give us the full error.
Trying to get property 'user_id' of non-object
is error
And you are using the updated code? Are you sure that the user has a role (at least one)?
dd(auth()->user()->sondazhe);
Yes i use updated code. And i thik yes
Role model
public function users()
{
return $this->belongsToMany(User::class);
}
User model
public function roles()
{
return $this->belongsToMany(Role::class);
}
If i do this
<div>
<h3>
:: {{ dd(auth()->user()->sondazhe) }}
</h3>
</div>
In view i see my table and
Total Sondazhe:
2
::
null
In your first post the method was named sondazhe() but now its called roles() ?
On user model i have both
public function sondazhes() {
return $this->hasMany(Role::class);
}
public function roles()
{
return $this->belongsToMany(Role::class);
}
Ok. So lets just ignore roles()
Try this and check in your database if the roles table has any roles with that user_id
dd(auth()->user()->id);
Role table have no user_id . I have user_id column on role_user table
This is migration for role table
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
Then it should be
public function sondazhes()
{
return $this->belongsToMany(Role::class);
}
Buy why do you have both roles and sondazhes ?
Couse i think i need many to many relatonship to connect user table with sondazhes table and then i can get data from sondazhes to users. is what i think
but if i try this code and remove role()
i get this error :::: Call to a member function pluck() on null
Ok.. So there is no direct relation between a user and sondazhes ?
One solution would be to load the sondazhes through the roles
$user = auth()->user();
$user->load('roles.sondazhes');
dd($user->roles->first()->sondazhes);
User is table with users i register on admin panel.
Sondazhes is another table when i get some data from db
Now i am trying , when i register with a user to acces just one piece of data, what belong to this user.
i aded this function on controller when i return view from sondazhes. and i get this https://prnt.sc/1rlyeaq
Please sign in or create an account to participate in this conversation.