vendredi 2 mars 2018

Load extra table/data into user object

In Laravel 5.6 I'm trying to load data into the user object, so I can view user credentials/settings etc.

Whats annoying is I had it working, but for some reason now It seems to have stopped, and I'm not sure what I've changed to break it.

Anyway I want to load two tables, access and settings. Both of them have user_id field in there with the corresponding user_id in.

In my User.php class I have two functions:

public function access() {
    return $this->hasMany(Access::class);   
}
public function settings() {
    return $this->hasOne(Settings::class);
}

I am not Use-ing them at the top of the class (i.e. use \App\Access) if that makes any difference.

And then the Access class looks like:

namespace App;
use Illuminate\Database\Eloquent\Model;
class Access extends Model
{
    protected $table = "access";
}

And the Settings class is very much the same:

namespace App;
use Illuminate\Database\Eloquent\Model;
class Settings extends Model
{
    protected $table = "settings";
}

However whenever I try and access Auth::user()->settings or Auth::user()->access I get undefined index: error. It's frustrating because like I said I had it working the other day and I'm not sure what's changed.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire