draw134
draw134

Reputation: 1187

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$status. Laravel 5.5 relationships

We are making a laravel project that is for schools. We got a database for schools and inside it are the the users like teachers,staffs ect. My problem is how do I prevent the specific user from a specific school from logging in where their school status is marked = 0 or as inactive in the schools database? I already get the authenticated function from vendor but it doesnt work. Is my relationships wrong?

The status is in the schools table and not in the users table but I already defined the relationships. Look at my models.

User.php

public function schools()
    {
        return $this->belongsTo('App\School', 'school_id');
    }

School.php

  public function user(){
        return $this->hasMany('App\User');
    }

LoginController.php


  use AuthenticatesUsers;
  public function authenticated(Request $request)
    {

        $user = User::find(Auth::user()->id);
            //  dd($user->schools());
            if($user->schools()->status == 1){

                dd('active ');

            }
                dd('inactive');

Upvotes: 1

Views: 604

Answers (1)

Salim Djerbouh
Salim Djerbouh

Reputation: 11044

Call the property status in an object retreived from the eloquent relationship hasMany not the relationship itself

$user = User::find(Auth::user()->id);
if($user->schools->status === 1) {
     dd('active');
}

Upvotes: 1

Related Questions