1

So I have the "User" model and "usersetting" model. the "usersettings" model has a column called "users_id" key to link it back to the User Model.

I'm using the spatie permissions and role module...

So in the Users model I have

use Spatie\Permission\Traits\HasRoles;


class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use HasRoles;
......
public function usersettings()
    {
        #return $this->hasOne('\App\usersettings','users_id');
        return $this->belongsTo('\App\usersettings','users_id');
    }
}

In the user setting model I have

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasRoles;

class usersettings extends Model
{
    use HasRoles;

    protected $guard_name = 'web'; // or whatever guard you want to use
        protected $table = 'usersettings';

        protected $fillable = [
        'users_id','stripe_customer',
    ];

    //
}

And in my Controller I have

public function chooseUser(Request $request){

        $theuserID = $request->input('id');
        \Log::info('The User ID:'.$theuserID);
        $whichuser = \App\User::find($theuserID)->with('usersettings')->get();
        $whichuser2 = \App\User::find($theuserID);

        /*if($isadmin = $whichuser2->hasRole('admin')){
            $whichuser->admin ='isadmin';
        }*/

        #\Log::info('THE HAVE:'.$isadmin);

        \Log::info('THE USER:'.$whichuser);
        #$roles = $whichuser->getRoleNames();
        #\Log::info('THE USER:'.$roles);

        return $whichuser;
    }

My GOAL is to return all the user information with the user settings as well as looking up whether or not the user has the admin role. I haven't been able to figure out how to put the three of these together.

I'm thinking that it may be a join instead of a realtion but then I dont' have a uUser model anymore and can't use the methods in the spatie package.

Thanks R

apokryfos
  • 38,771
  • 9
  • 70
  • 114
BostonMacOSX
  • 1,369
  • 2
  • 17
  • 38

1 Answers1

0

The are some problems you need to resolve.

  1. User - User Settings (One - Many Relationship)

You can think user has many user settings, and user settings belong to user.

In User Model, you should define this relationship:

public function usersettings() {
    return $this->hasMany('\App\usersettings', 'users_id');
}

In User Settings Model, you should define this inverse relationship:

public function user() {
    return $this->belongsTo('App\User', 'users_id');
}
  1. Querying Relationship

When you accessing relationship, you may simply access the relationship as if it were a property. If you access the usersettings relationship, the return type is Illuminate\Database\Eloquent\Collection.

$whichuser = \App\User::find($theuserID);
$userSettings = $whichuser->usersettings;
  1. Eager Loading

You are using with in an incorrect way, you should check eager loading:

When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property. However, Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates the N + 1 query problem.

  1. Role / Permission

This is the correct way to check whether the user has the specific role. You should fully read spatie/laravel-permission to understand their usage.

if ($whichuser2->hasRole('admin')) { ... }
Ben
  • 5,069
  • 4
  • 18
  • 26