简体   繁体   中英

Laravel Error on eager loading relations when migrated from 5.1 to 5.3

User.php 

namespace ESP;

use Illuminate\Auth\Authenticatable; 

use Illuminate\Database\Eloquent\Model; 

use Illuminate\Auth\Passwords\CanResetPassword; 

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use DB;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract 
{

    use Authenticatable, CanResetPassword;

    protected $table = 'users';

    protected $primaryKey = 'id';

    public function companies(){
           return $this->belongsToMany('ESP\Company', 'company_user', 'user_id', 'company_id');
    }

   public function scopeUsername($query, $username){

    return $query->where('username', '=', $username);
   }
}


Company.php 

namespace ESP;

use Illuminate\Database\Eloquent\Model; use DB;

class Company extends Model {

    protected $table = 'companies';

    protected $primaryKey = 'id';

    public function users(){
        return $this->belongsToMany('ESP\User');
    }
}

LoginController.php

namespace ESP\Http\Controllers\Login;

use ESP\Http\Requests;

use ESP\Http\Controllers\Controller; use ESP\CustomClass\LDAP\adLDAP;

use Illuminate\Http\Request; 

use Illuminate\Support\Facades\Redirect;

use ESP\LoginAttempts; 

use ESP\Helpers\UtilityHelper;

use ESP\User;

use DB; 

use Auth; 

use Session; 

use View; 

use Paginator; 

use DateTime; 

use Input;

class LoginController extends Controller 
{

    private function _authenticateClient($username, $password)
    {
        $g = User::with('companies')
            ->where( 'username', $username )
            ->where( 'password', md5( $password ) )
            ->get();
        dd($g);
    }
}

Error has something like this.

BadMethodCallException in Builder.php line 2405: Call to undefined method Illuminate\Database\Query\Builder::companies()

I can't even make use of the local scope Username on my controller call.

This models are working perfectly fine on my laravel 5.1. But im trying to migrate my codebase to 5.3 and im stuck with this issues.

You can check app/config/auth.php

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\ESP\User::class,
    ]
],

make sure you registered your user model correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM