简体   繁体   English

从5.1迁移到5.3时,Laravel在渴望加载关系时出错

[英]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. 该模型在我的laravel 5.1上运行良好。 But im trying to migrate my codebase to 5.3 and im stuck with this issues. 但是我试图将我的代码库迁移到5.3,并坚持使用此问题。

You can check app/config/auth.php 您可以检查app / config / auth.php

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

make sure you registered your user model correctly. 确保您正确注册了用户模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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