Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

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

        }
0 likes
15 replies
Sinnbeck's avatar

Did you mean to do

auth()->user()->sondazhe->first()->user_id 
Maison012's avatar

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
Sinnbeck's avatar

Updated.. And remember to give us the full error.

Maison012's avatar

Trying to get property 'user_id' of non-object

is error

Sinnbeck's avatar

And you are using the updated code? Are you sure that the user has a role (at least one)?

dd(auth()->user()->sondazhe);
Maison012's avatar

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

    }
Maison012's avatar

If i do this

		<div>
        <h3>
           :: {{  dd(auth()->user()->sondazhe) }}
        </h3>
        </div>

In view i see my table and

        Total Sondazhe:  
        2
        ::
        null
Sinnbeck's avatar

In your first post the method was named sondazhe() but now its called roles() ?

Maison012's avatar

On user model i have both

  public function sondazhes() {
    return $this->hasMany(Role::class);
 }



  public function roles()
{
    return $this->belongsToMany(Role::class);

}
Sinnbeck's avatar

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);
Maison012's avatar

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();

        });

          }
Sinnbeck's avatar

Then it should be

  public function sondazhes()
{
    return $this->belongsToMany(Role::class);

}

Buy why do you have both roles and sondazhes ?

Maison012's avatar
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

Sinnbeck's avatar

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);
Maison012's avatar

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 or to participate in this conversation.