Home > front end >  I cant retrieve id from user in the database
I cant retrieve id from user in the database

Time:01-03

When i use this code in my User model

public function get_user_by_email($email)   {
        $data = $this->where('email', $email);
        return $data->id;
    }

I get this error

Property [id] does not exist on the Eloquent builder instance.

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1602
    1598▕         if ($key === 'orWhere') {
    1599▕             return new HigherOrderBuilderProxy($this, $key);
    1600▕         }
    1601▕ 
  ➜ 1602▕         throw new Exception("Property [{$key}] does not exist on the Eloquent builder instance.");
    1603▕     }
    1604▕ 
    1605▕     /**
    1606▕      * Dynamically handle calls into the query instance.

  1   app/Models/User.php:64
      Illuminate\Database\Eloquent\Builder::__get()

  2   app/Models/invite.php:21
      App\Models\User::get_user_by_email()

Please help the code should work and i have filled my database with dummy users. why cant i get my user id from the user model. I have used jetstream for this

CodePudding user response:

You need to use first() on the Eloquent Builder to return the Model before you can access its attributes.

$data = $this->where('email', $email)->first();
return $data->id;

CodePudding user response:

You can try in this way.

$data = Model::where('email', $email)->pluck('id');
  • Related